From 64209738de41ae114998964f293c7ec408f4d7af Mon Sep 17 00:00:00 2001 From: projectmoon Date: Mon, 5 Feb 2024 21:24:57 +0100 Subject: [PATCH] GBNF: const generics for arrays; lock derive to owned data. Remove the trait impl macros for arrays, as we can use const generics for it instead. Add DeserializeOwned trait bound to the blanket trait impls and clarify the panic message that the derive macro only works with structs that have owned data (no borrows). --- gbnf/src/lib.rs | 59 ++++++++++++++---------------------------- gbnf_derive/src/lib.rs | 2 +- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/gbnf/src/lib.rs b/gbnf/src/lib.rs index b7526b7..aa49b2e 100644 --- a/gbnf/src/lib.rs +++ b/gbnf/src/lib.rs @@ -45,27 +45,6 @@ macro_rules! define_field_type { }; } -macro_rules! define_array_blanket_impl { - ($len:expr) => { - impl AsGbnf for [T; $len] - where - T: AsGbnf + DeserializeOwned, - { - fn to_gbnf() -> GbnfFieldType { - use GbnfFieldType::*; - match ::to_gbnf() { - Primitive(primitive_type) => PrimitiveList(primitive_type), - OptionalPrimitive(primitive_type) => PrimitiveList(primitive_type), - Complex(complex_type) => ComplexList(complex_type), - OptionalComplex(complex_type) => ComplexList(complex_type), - Limited(_) => panic!("limited values are not yet supported"), - ComplexList(_) | PrimitiveList(_) => panic!("nested lists not supported"), - } - } - } - }; -} - #[macro_export] macro_rules! gbnf_field_type { ($type:ty) => { @@ -99,27 +78,27 @@ define_field_type!(bool, GbnfFieldType::Primitive(GbnfPrimitive::Boolean)); define_field_type!(String, GbnfFieldType::Primitive(GbnfPrimitive::String)); define_field_type!(char, GbnfFieldType::Primitive(GbnfPrimitive::String)); -// Macro-based blanket impls for arrays -define_array_blanket_impl!(1); -define_array_blanket_impl!(3); -define_array_blanket_impl!(4); -define_array_blanket_impl!(5); -define_array_blanket_impl!(6); -define_array_blanket_impl!(7); -define_array_blanket_impl!(8); -define_array_blanket_impl!(9); -define_array_blanket_impl!(10); -define_array_blanket_impl!(11); -define_array_blanket_impl!(12); -define_array_blanket_impl!(13); -define_array_blanket_impl!(14); -define_array_blanket_impl!(15); -define_array_blanket_impl!(16); - // Blanket implementations to cover more types +impl AsGbnf for [T; N] +where + T: AsGbnf + DeserializeOwned, +{ + fn to_gbnf() -> GbnfFieldType { + use GbnfFieldType::*; + match ::to_gbnf() { + Primitive(primitive_type) => PrimitiveList(primitive_type), + OptionalPrimitive(primitive_type) => PrimitiveList(primitive_type), + Complex(complex_type) => ComplexList(complex_type), + OptionalComplex(complex_type) => ComplexList(complex_type), + Limited(_) => panic!("limited values are not yet supported"), + ComplexList(_) | PrimitiveList(_) => panic!("nested lists not supported"), + } + } +} + impl AsGbnf for Vec where - T: AsGbnf, + T: AsGbnf + DeserializeOwned, { fn to_gbnf() -> GbnfFieldType { use GbnfFieldType::*; @@ -136,7 +115,7 @@ where impl AsGbnf for Option where - T: AsGbnf, + T: AsGbnf + DeserializeOwned, { fn to_gbnf() -> GbnfFieldType { use GbnfFieldType::*; diff --git a/gbnf_derive/src/lib.rs b/gbnf_derive/src/lib.rs index 9d284aa..33ddd89 100644 --- a/gbnf_derive/src/lib.rs +++ b/gbnf_derive/src/lib.rs @@ -89,7 +89,7 @@ fn generate_gbnf(input: TokenStream, create_struct: bool) -> TokenStream { code.into() } else { - panic!("Can only generate GBNF from structs (pub or private)"); + panic!("Can only generate GBNF from structs of owned data (pub or private)"); } }