[−][src]Macro structural::z_impl_from_structural
For implementing FromStructural
,
and delegating the implementation of TryFromStructural
to it.
Example
This example demonstrates how you can implement FromStructural
in a
more general way than necessary,
use structural::{FP, IntoField, Structural, StructuralExt, fp, make_struct}; use std::borrow::Cow; { let this = make_struct!{ encoding: "utf8", contents: &[0,1,2,3,4][..], }; assert_eq!( this.into_struc::<Message>(), Message{encoding: "utf8".to_string(), contents: vec![0,1,2,3,4]} ); } { let this = HttpMessage{ encoding: Cow::from("utf16"), contents: Cow::from(vec![5,7,8]), valid_until: 0o40002, }; assert_eq!( this.into_struc::<Message>(), Message{encoding: "utf16".to_string(), contents: vec![5,7,8]} ); } #[derive(Structural, Debug)] #[struc(no_trait, public, access="move")] pub struct HttpMessage<'a> { encoding: Cow<'a,str>, contents: Cow<'a,[u8]>, valid_until: u32, } #[derive(Structural, Debug, PartialEq)] #[struc(no_trait, public, access="move")] pub struct Message { encoding: String, contents: Vec<u8>, } // This macro generates the TryFromStructural impl based on the passed // FromStructural implementation. structural::z_impl_from_structural! { impl[F, E, C] FromStructural<F> for Message where[ // The bounds here would usually just be `F: Message_SI` // (Message_SI being a trait generated by the Structural derive, // aliasing the accessor traits implemented by Message), // but I decided to make this example different. F: IntoField<FP!(encoding), Ty = C>, F: IntoField<FP!(contents), Ty = E>, C: Into<String>, E: Into<Vec<u8>>, ]{ fn from_structural(this){ let (encoding, contents) = this.into_fields(fp!(encoding, contents)); Self { encoding: encoding.into(), contents: contents.into(), } } } }