Skip to content

Commit

Permalink
Rework to use initialize using iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanderc committed Oct 20, 2021
1 parent 52b4c4a commit 12bd26b
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 89 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dyn_struct"
version = "0.1.0"
version = "0.2.0"
edition = "2018"
authors = ["Christofer Nolander <christofer.nolander@gmail.com>"]
repository = "https://github.com/nolanderc/dyn_struct"
Expand All @@ -12,4 +12,4 @@ default = ["derive"]
derive = ["dyn_struct_derive"]

[dependencies]
dyn_struct_derive = { version = "0.1.0", path = "derive", optional = true }
dyn_struct_derive = { version = "0.2.0", path = "derive", optional = true }
2 changes: 1 addition & 1 deletion derive/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dyn_struct_derive"
version = "0.1.0"
version = "0.2.0"
edition = "2018"
authors = ["Christofer Nolander <christofer.nolander@gmail.com>"]
repository = "https://github.com/nolanderc/dyn_struct"
Expand Down
56 changes: 26 additions & 30 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ fn expand(input: syn::DeriveInput) -> syn::Result<TokenStream> {
}
syn::GenericParam::Lifetime(life) => {
let lifetime = &life.lifetime;
quote!{ &#lifetime () }
},
quote! { &#lifetime () }
}
syn::GenericParam::Const(constant) => {
let ident = &constant.ident;
quote!{ [(); #ident] }
},
quote! { [(); #ident] }
}
});

phantom_field = quote! {
Expand Down Expand Up @@ -97,16 +97,32 @@ fn expand(input: syn::DeriveInput) -> syn::Result<TokenStream> {
quote! { #name: #ty }
});

let ident = &input.ident;
let dynamic_type = &dynamic_field.ty;
let dynamic_type = match &dynamic_field.ty {
syn::Type::Slice(inner) => inner.elem.as_ref(),
_ => {
return Err(err!(
dynamic_field.ty,
"the last field needs to be a slice `[T]`"
))
}
};
let dynamic_name = dynamic_field
.ident
.clone()
.unwrap_or_else(|| syn::Ident::new("tail", span(dynamic_type)));

let struct_ident = &input.ident;
Ok(quote! {
impl #impl_generics #ident #type_generics #where_clause {
pub fn new(#(#sized_parameters,)* dynamic: &#dynamic_type) -> Box<Self> {
impl #impl_generics #struct_ident #type_generics #where_clause {
pub fn new<I>(#(#sized_parameters,)* #dynamic_name: I) -> Box<Self>
where I: std::iter::IntoIterator<Item = #dynamic_type>,
<I as std::iter::IntoIterator>::IntoIter: std::iter::ExactSizeIterator
{
#single_definition

let single: #single #type_generics = #single_init;
let header: #single #type_generics = #single_init;

let dyn_struct = dyn_struct::DynStruct::new(single, dynamic);
let dyn_struct = dyn_struct::DynStruct::new(header, #dynamic_name);
let ptr = std::boxed::Box::into_raw(dyn_struct);
unsafe { std::boxed::Box::from_raw(ptr as *mut Self) }
}
Expand Down Expand Up @@ -175,23 +191,3 @@ fn find_ident(tokens: TokenStream) -> Option<syn::Ident> {
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn simple() {
let input = quote! {
#[repr(C)]
struct Foo<T, U> {
value: u32,
list: [u32],
}
}
.into();
let input = syn::parse2::<syn::DeriveInput>(input).unwrap();
let output = expand(input).unwrap();

assert_eq!(output.to_string(), quote! {}.to_string());
}
}
Loading

0 comments on commit 12bd26b

Please sign in to comment.