mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Make into_struct_sequence not clone fields
This commit is contained in:
2 files changed
+14
-33
No files matched your search
+10
-29
@@ -1,7 +1,7 @@
|
||||
use super::Diagnostic;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, Index};
|
||||
use syn::DeriveInput;
|
||||
|
||||
pub(crate) fn impl_pystruct_sequence(
|
||||
input: DeriveInput,
|
||||
@@ -15,44 +15,25 @@ pub(crate) fn impl_pystruct_sequence(
|
||||
)
|
||||
};
|
||||
|
||||
let mut properties = Vec::new();
|
||||
let mut field_names = Vec::new();
|
||||
match fields {
|
||||
syn::Fields::Named(fields) => {
|
||||
for (i, field) in fields.named.iter().enumerate() {
|
||||
let idx = Index::from(i);
|
||||
let field_name = field.ident.as_ref().unwrap();
|
||||
let field_name_str = field_name.to_string();
|
||||
// TODO add doc to the generated property
|
||||
let property = quote! {
|
||||
class.set_str_attr(
|
||||
#field_name_str,
|
||||
ctx.new_readonly_getset(
|
||||
#field_name_str,
|
||||
|zelf: &::rustpython_vm::obj::objtuple::PyTuple,
|
||||
_vm: &::rustpython_vm::VirtualMachine| {
|
||||
zelf.fast_getitem(#idx)
|
||||
}
|
||||
),
|
||||
);
|
||||
};
|
||||
properties.push(property);
|
||||
field_names.push(quote!(#field_name));
|
||||
}
|
||||
}
|
||||
let field_names: Vec<_> = match fields {
|
||||
syn::Fields::Named(fields) => fields
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| field.ident.as_ref().unwrap())
|
||||
.collect(),
|
||||
_ => bail_span!(
|
||||
input,
|
||||
"#[pystruct_sequence] can only be on a struct with named fields"
|
||||
),
|
||||
}
|
||||
};
|
||||
|
||||
let ty = &input.ident;
|
||||
let ret = quote! {
|
||||
impl ::rustpython_vm::pyobject::PyStructSequence for #ty {
|
||||
const FIELD_NAMES: &'static [&'static str] = &[#(stringify!(#field_names)),*];
|
||||
fn to_tuple(&self, vm: &::rustpython_vm::VirtualMachine,) -> ::rustpython_vm::obj::objtuple::PyTuple {
|
||||
fn into_tuple(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::obj::objtuple::PyTuple {
|
||||
let items = vec![#(::rustpython_vm::pyobject::IntoPyObject::into_pyobject(
|
||||
::std::clone::Clone::clone(&self.#field_names),
|
||||
self.#field_names,
|
||||
vm,
|
||||
)),*];
|
||||
items.into()
|
||||
|
||||
+4
-4
@@ -1336,13 +1336,13 @@ pub trait PyClassImpl: PyClassDef {
|
||||
}
|
||||
|
||||
#[pyimpl]
|
||||
pub trait PyStructSequence: PyClassImpl + 'static {
|
||||
pub trait PyStructSequence: PyClassImpl + Sized + 'static {
|
||||
const FIELD_NAMES: &'static [&'static str];
|
||||
|
||||
fn to_tuple(&self, vm: &VirtualMachine) -> PyTuple;
|
||||
fn into_tuple(self, vm: &VirtualMachine) -> PyTuple;
|
||||
|
||||
fn into_struct_sequence(&self, vm: &VirtualMachine, cls: PyClassRef) -> PyResult<PyTupleRef> {
|
||||
self.to_tuple(vm).into_ref_with_type(vm, cls)
|
||||
fn into_struct_sequence(self, vm: &VirtualMachine, cls: PyClassRef) -> PyResult<PyTupleRef> {
|
||||
self.into_tuple(vm).into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
|
||||
Reference in new issue
Block a user