From 43b7a7bdbe1064fd6c59f9df41d7daf90ee8a626 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sun, 30 Aug 2020 14:55:16 -0500 Subject: [PATCH] Make into_struct_sequence not clone fields --- derive/src/pystructseq.rs | 39 ++++++++++----------------------------- vm/src/pyobject.rs | 8 ++++---- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/derive/src/pystructseq.rs b/derive/src/pystructseq.rs index d5286ffd9..5c8da6510 100644 --- a/derive/src/pystructseq.rs +++ b/derive/src/pystructseq.rs @@ -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() diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 753b1c496..62fbed9c3 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -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 { - self.to_tuple(vm).into_ref_with_type(vm, cls) + fn into_struct_sequence(self, vm: &VirtualMachine, cls: PyClassRef) -> PyResult { + self.into_tuple(vm).into_ref_with_type(vm, cls) } #[pymethod(magic)]