#[repr(transparent)]pub struct StrucWrapper<T>(pub T);
Expand description
A wrapper type alternative to StructuralExt
,
with methods for accessing fields in structural types.
§Example: Struct
use structural::{StrucWrapper, Structural, fp};
use structural::structural_aliases::Array3;
let mut this=Point{x:3, y:5, z:8};
let mut tuple=(13,21,34);
rotate_tuple(&mut this);
rotate_tuple(&mut tuple);
assert_eq!( this, Point{x:8, y:3, z:5} );
assert_eq!( tuple, (34,13,21) );
fn rotate_tuple(tuple: &mut dyn Array3<u32>){
use std::mem::swap;
let mut tuple=StrucWrapper(tuple);
let (a,b,c)=tuple.muts(fp!(0,1,2));
swap(b,c);
swap(a,b);
}
#[derive(Debug,Structural,PartialEq)]
struct Point{
#[struc(rename="0")]
pub x: u32,
#[struc(rename="1")]
pub y: u32,
#[struc(rename="2")]
pub z: u32,
}
§Example: Enum
use structural::{StrucWrapper, Structural, fp};
assert_eq!( get_value(States::Initial), 1 );
assert_eq!( get_value(States::Open{how_much: 10}), 160+2 );
assert_eq!( get_value(States::Closed), 3 );
assert_eq!( get_value(UberStates::Initial), 1 );
assert_eq!( get_value(UberStates::Open{how_much: 10, throughput: 14}), 160+2 );
assert_eq!( get_value(UberStates::Open{how_much: 20, throughput: 55}), 320+2 );
assert_eq!( get_value(UberStates::Closed), 3 );
// `States_SI` was declared by the `Structural` derive macro on
// the `States` enum,aliasing its accessor trait impls.
fn get_value(this: impl States_SI)-> u64 {
let this=StrucWrapper(this);
if this.is_variant(fp!(Initial)) {
1
}else if let Some(how_much)= this.r(fp!(::Open.how_much)) {
2 + ((*how_much as u64) << 4)
}else if this.is_variant(fp!(Closed)) {
3
}else{
0
}
}
// This function is equivalent to `get_value`
//
// `States_SI` was declared by the `Structural` derive macro on
// the `States` enum,aliasing its accessor trait impls.
fn get_value_switch(this: impl States_SI)-> u64 {
structural::switch!{ref this;
Initial=>1,
Open{&how_much}=>2 + ((how_much as u64) << 4),
Closed=>3,
_=>0,
}
}
#[derive(Structural)]
enum States{
Initial,
Open{how_much: u32},
Closed,
}
#[derive(Structural)]
enum UberStates{
Initial,
Open{
how_much: u32,
throughput: u64,
},
Closed,
}
Tuple Fields§
§0: T
Implementations§
Source§impl<T> StrucWrapper<T>
impl<T> StrucWrapper<T>
Sourcepub fn r<'a, P>(
&'a self,
path: P,
) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
pub fn r<'a, P>( &'a self, path: P, ) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
Gets a reference to a single field,determined by path
.
This function is equivalent to StructuralExt::field_
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, fp};
use structural::structural_aliases::Array5;
assertions((0,0,13,0,34));
assertions((0,0,13,0,34,""));
assertions((0,0,13,0,34,"",false));
assertions([0,0,13,0,34]);
assertions([0,0,13,0,34,0]);
assertions([0,0,13,0,34,0,0]);
fn assertions(this: impl Array5<u64>){
let mut this=StrucWrapper(this);
assert_eq!( this.r(fp!(2)), &13 );
assert_eq!( this.r(fp!(4)), &34 );
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{
EnumWithNewtype, EnumWithoutNewtype, EnumWithNewtype_SI, RefWrapper,
};
assertions(EnumWithNewtype::U32(RefWrapper(0x100, &43370)));
assertions(EnumWithoutNewtype::U32(0x100, &43370));
// `EnumWithNewtype_SI` was declared by the `Structural` derive macro on
// the `EnumWithNewtype` enum,aliasing its accessor trait impls.
fn assertions<'a>(this: impl EnumWithNewtype_SI<'a>){
let mut this=StrucWrapper(this);
assert_eq!( this.r(fp!(::U32.0)), Some(&0x100) );
assert_eq!( this.r(fp!(::U32.1)), Some(&&43370) );
assert_eq!( this.r(fp!(::U64.0)), None );
}
Sourcepub fn m<'a, P>(
&'a mut self,
path: P,
) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
pub fn m<'a, P>( &'a mut self, path: P, ) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
Gets a mutable reference to a single field,determined by path
.
This function is equivalent to StructuralExt::field_mut
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, Structural, fp, make_struct};
assertions(Puck{name:"John", surname:"Chickenbert"});
assertions(make_struct!{
#![derive(Debug,Copy,Clone)]
name:"John",
surname:"Chickenbert"
});
// `Puck_SI` was declared by the `Structural` derive macro on the Puck struct,
// aliasing its accessor trait impls.
fn assertions(this: impl Puck_SI + Copy){
let mut this=StrucWrapper(this);
assert_eq!( this.m(fp!(name)), &mut "John" );
assert_eq!( this.m(fp!(surname)), &mut "Chickenbert" );
}
#[derive(Structural,Copy,Clone)]
pub struct Puck{
pub name: &'static str,
pub surname: &'static str,
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{
EnumWithNewtype, EnumWithoutNewtype, EnumWithNewtype_SI, RefWrapper,
};
assertions(EnumWithNewtype::U32(RefWrapper(0x100, &43370)));
assertions(EnumWithoutNewtype::U32(0x100, &43370));
// `EnumWithNewtype_SI` was declared by the `Structural` derive macro on
// the `EnumWithNewtype` enum,aliasing its accessor trait impls.
fn assertions<'a>(this: impl EnumWithNewtype_SI<'a>){
let mut this=StrucWrapper(this);
assert_eq!( this.m(fp!(::U32.0)), Some(&mut 0x100) );
assert_eq!( this.m(fp!(::U32.1)), Some(&mut &43370) );
assert_eq!( this.m(fp!(::U64.0)), None );
}
Sourcepub fn v<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
pub fn v<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
Converts this into a single field by value,determined by path
.
This function is equivalent to StructuralExt::into_field
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, Structural, fp, make_struct};
assertions(Puck{name:"John", surname:"Chickenbert"});
assertions(make_struct!{
#![derive(Debug,Copy,Clone)]
name:"John",
surname:"Chickenbert"
});
// `Puck_SI` was declared by the `Structural` derive macro on the Puck struct,
// aliasing its accessor trait impls.
fn assertions(this: impl Puck_SI + Copy){
let this=StrucWrapper(this);
assert_eq!( this.v(fp!(name)), "John" );
assert_eq!( this.v(fp!(surname)), "Chickenbert" );
}
#[derive(Structural,Copy,Clone)]
pub struct Puck{
pub name: &'static str,
pub surname: &'static str,
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{WithBoom, WithBoom_SI, Bomb};
assertions(WithBoom::Boom{a:"#eh#", b:&[5,8,13]});
assertions( Bomb::Boom{a:"#eh#", b:&[5,8,13]});
// `WithBoom_SI` was declared by the `Structural` derive macro on the `WithBoom` enum,
// aliasing its accessor trait impls.
fn assertions(this: impl WithBoom_SI + Copy){
let this=StrucWrapper(this);
assert_eq!( this.v(fp!(::Boom.a)), Some("#eh#") );
assert_eq!( this.v(fp!(::Boom.b)), Some(&[5,8,13][..]) );
assert!( this.v(fp!(::Nope)).is_none() );
}
Sourcepub fn refs<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, T>where
P: RevGetMultiField<'a, T>,
pub fn refs<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, T>where
P: RevGetMultiField<'a, T>,
Gets references to multiple fields,determined by path
.
This function is equivalent to StructuralExt::fields
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, fp, make_struct};
use structural::for_examples::{Struct2, Struct2_SI, Struct3};
assertions(Struct2{foo:Some("&"), bar:(true,false)});
assertions(Struct3{foo:Some("&"), bar:(true,false), baz:&[()]});
assertions(make_struct!{foo:Some("&"), bar:(true,false), boom:()});
// `Struct2_SI` was declared by the `Structural` derive macro on the Struct2 struct,
// aliasing its accessor trait impls.
fn assertions(this: impl Struct2_SI<&'static str, (bool,bool)>){
let this=StrucWrapper(this);
assert_eq!( this.refs(fp!(foo,bar)), (&Some("&"), &(true,false)) );
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{WithBoom, WithBoom_SI, Bomb};
assertions(WithBoom::Boom{a:"#eh#", b:&[5,8,13]});
assertions( Bomb::Boom{a:"#eh#", b:&[5,8,13]});
// `WithBoom_SI` was declared by the `Structural` derive macro on the `WithBoom` enum,
// aliasing its accessor trait impls.
fn assertions(this: impl WithBoom_SI){
let this=StrucWrapper(this);
assert_eq!( this.refs(fp!(::Boom=>a,b)), Some((&"#eh#", &&[5,8,13][..])) );
assert!( this.refs(fp!(::Nope=>)).is_none() );
}
Sourcepub fn clones<'a, P>(
&'a self,
path: P,
) -> ClonedOut<RevGetMultiFieldOut<'a, P, T>>
pub fn clones<'a, P>( &'a self, path: P, ) -> ClonedOut<RevGetMultiFieldOut<'a, P, T>>
Gets clones of multiple fields,determined by path
.
This function is equivalent to StructuralExt::cloned_fields
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, fp, make_struct};
use structural::for_examples::{Struct2, Struct2_SI, Struct3};
assertions(Struct2{foo:Some("&"), bar:(true,false)});
assertions(Struct3{foo:Some("&"), bar:(true,false), baz:&[()]});
assertions(make_struct!{foo:Some("&"), bar:(true,false), boom:()});
fn assertions(this: impl Struct2_SI<&'static str, (bool,bool)>){
let this=StrucWrapper(this);
assert_eq!( this.clones(fp!(foo,bar)), (Some("&"), (true,false)) );
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{Enum2, Enum2_SI, Enum3, Enum4};
use std::cmp::Ordering;
assertions(Enum2::Bar(Ordering::Less, Some(1337)));
assertions(Enum3::Bar(Ordering::Less, Some(1337)));
assertions(Enum4::Bar(Ordering::Less, Some(1337)));
fn assertions(this: impl Enum2_SI){
let this=StrucWrapper(this);
assert_eq!( this.clones(fp!(::Bar=>0,1)), Some((Ordering::Less, Some(1337))) );
assert_eq!( this.clones(fp!(::Foo=>0,1)), None );
}
Sourcepub fn muts<'a, P>(&'a mut self, path: P) -> RevGetMultiFieldMutOut<'a, P, T>where
P: RevGetMultiFieldMut<'a, T>,
pub fn muts<'a, P>(&'a mut self, path: P) -> RevGetMultiFieldMutOut<'a, P, T>where
P: RevGetMultiFieldMut<'a, T>,
Gets mutable references to multiple fields,determined by path
.
This function is equivalent to StructuralExt::fields_mut
,
which has more complete examples.
§Example: Struct
use structural::{StrucWrapper, fp};
use structural::structural_aliases::Array5;
assertions((0,0,8,0,21));
assertions((0,0,8,0,21,""));
assertions((0,0,8,0,21,"",false));
assertions([0,0,8,0,21]);
assertions([0,0,8,0,21,0]);
assertions([0,0,8,0,21,0,0]);
fn assertions(this: impl Array5<u64>){
let mut this=StrucWrapper(this);
assert_eq!( this.muts(fp!(2,4)), (&mut 8, &mut 21) );
}
§Example: Enum
use structural::{StrucWrapper, fp};
use structural::for_examples::{Enum2, Enum2_SI, Enum3, Enum4};
assertions(Enum2::Foo(27, 81));
assertions(Enum3::Foo(27, 81));
assertions(Enum4::Foo(27, 81));
fn assertions(this: impl Enum2_SI){
let mut this=StrucWrapper(this);
assert_eq!( this.muts(fp!(::Foo=>0,1)), Some((&mut 27, &mut 81)) );
assert_eq!( this.muts(fp!(::Bar=>0)), None );
}
Sourcepub fn vals<P>(self, path: P) -> RevIntoMultiFieldOut<P, T>where
P: RevIntoMultiField<T>,
pub fn vals<P>(self, path: P) -> RevIntoMultiFieldOut<P, T>where
P: RevIntoMultiField<T>,
Converts this into multiple fields by value, determined by path.
This function is equivalent to StructuralExt::into_fields
,
which has more complete documentation.
§Valid Paths
As opposed to the other multiple fields accessor methods, this method only accepts field paths that refer to multiple non-nested fields inside some value (possibly a nested field itself).
For examples of valid and invalid paths to pass as parameter look here.
§Example: Struct
use structural::{StrucWrapper, Structural, fp, make_struct};
assert_eq!(
into_parts(Bicycle{
frame: Frame("wheeee"),
wheels: [Wheel("first"), Wheel("second")],
handle_bar: HandleBar("hands on me"),
}),
(Frame("wheeee"), [Wheel("first"), Wheel("second")], HandleBar("hands on me"))
);
assert_eq!(
into_parts(make_struct!{
frame: Frame("frame"),
wheels: [Wheel("1"), Wheel("4")],
handle_bar: HandleBar("9"),
}),
(Frame("frame"), [Wheel("1"), Wheel("4")], HandleBar("9"))
);
// The `Bicycle_SI` was generated for the `Bicycle` struct by the `Structural` derive,
// aliasing it's accessor traits
fn into_parts(this: impl Bicycle_SI)-> (Frame, [Wheel;2], HandleBar) {
let this = StrucWrapper(this);
this.vals(fp!(frame, wheels, handle_bar))
}
#[derive(Structural,Debug,Copy,Clone,PartialEq)]
struct Bicycle{
pub frame: Frame,
pub wheels: [Wheel;2],
pub handle_bar: HandleBar,
}
§Example: Enum
use structural::{StrucWrapper, Structural, TS, fp, make_struct};
let human=Human{name: "bob".into(), gold: 600};
assert_eq!( enum_into_human(Entities::Human(human.clone())), Some(human.clone()) );
assert_eq!(
enum_into_human(MoreEntities::Human{name: "John".into(), gold: 1234}),
Some(Human{name: "John".into(), gold: 1234})
);
// The `Human_VSI` trait was generated for `Human` by the `Structural` derive macro,
// to access variants with the same fields as Human.
// The `TS!(Human)` argument makes it require the variant to be `Human`.
fn enum_into_human(this: impl Human_VSI<TS!(Human)>)-> Option<Human> {
let this= StrucWrapper(this);
let (name, gold)= this.vals(fp!(::Human=>name,gold))?;
Some(Human{name, gold})
}
#[derive(Structural, Clone, Debug, PartialEq)]
pub struct Human{
pub name: String,
pub gold: u64,
}
#[derive(Structural, Clone, Debug, PartialEq)]
pub enum Entities {
#[struc(newtype(bounds = "Human_VSI<@variant>"))]
Human(Human),
Wolf,
}
#[derive(Structural, Clone, Debug, PartialEq)]
pub enum MoreEntities {
Human{
name: String,
gold: u64,
},
Wolf,
Cat,
Dog,
}
Sourcepub fn is_variant<P>(&self, _path: P) -> bool
pub fn is_variant<P>(&self, _path: P) -> bool
Queries whether an enum is a particular variant.
This function is equivalent to StructuralExt::is_variant
.
§Example
use structural::{StrucWrapper, Structural, fp};
assertions(
EnumOne::Bar,
EnumOne::Baz{x:0, y:100},
EnumOne::Qux("hello", "world"),
);
assertions(EnumTwo::Bar, EnumTwo::Baz, EnumTwo::Qux);
fn assertions<T>(bar: T, baz: T, qux: T)
where
T: EnumTwo_SI
{
let bar=StrucWrapper(bar);
let baz=StrucWrapper(baz);
let qux=StrucWrapper(qux);
assert!( bar.is_variant(fp!(Bar)) );
assert!( baz.is_variant(fp!(Baz)) );
assert!( qux.is_variant(fp!(Qux)) );
}
#[derive(Structural)]
enum EnumOne{
Bar,
Baz{
x:u32,
y:u32,
},
Qux(&'static str, &'static str),
}
#[derive(Structural)]
enum EnumTwo{
Bar,
Baz,
Qux,
}
Sourcepub fn into_struc<U>(self) -> Uwhere
T: IntoStructural<U>,
pub fn into_struc<U>(self) -> Uwhere
T: IntoStructural<U>,
Converts this into another structural type,using IntoStructural
.
§Struct Example
use structural::{
for_examples::{StructFoo, StructBar},
FP, IntoField, StrucWrapper, make_struct,
};
assert_eq!( into_foo(make_struct!{foo: 100}), StructFoo{foo: 100} );
assert_eq!( into_foo(make_struct!{foo: 200}), StructFoo{foo: 200} );
assert_eq!( into_bar(make_struct!{bar: 3}), StructBar{bar: 3} );
assert_eq!( into_bar(make_struct!{bar: 5}), StructBar{bar: 5} );
fn into_foo<T>(this: T)->StructFoo<T::Ty>
where
T: IntoField<FP!(foo)>
{
let this=StrucWrapper(this);
this.into_struc()
}
fn into_bar<T>(this: T)->StructBar<T::Ty>
where
T: IntoField<FP!(bar)>
{
let this=StrucWrapper(this);
this.into_struc()
}
§Enum Example
use structural::{
convert::{EmptyTryFromError, FromStructural, TryFromError, TryFromStructural},
for_examples::Enum3,
Structural, StrucWrapper, make_struct, switch,
};
use std::cmp::Ordering;
assert_eq!( into_v(Enum3::Foo(3, 8)), NoPayload::Foo );
assert_eq!( into_v(Enum3::Bar(Ordering::Less, None)), NoPayload::Bar );
assert_eq!( into_v(Enum3::Baz{foom: "what"}), NoPayload::Baz );
assert_eq!( into_v(WithPayload::Foo(13)), NoPayload::Foo );
assert_eq!( into_v(WithPayload::Bar(21)), NoPayload::Bar );
assert_eq!( into_v(WithPayload::Baz(34)), NoPayload::Baz );
assert_eq!( into_v(NoPayload::Foo), NoPayload::Foo );
assert_eq!( into_v(NoPayload::Bar), NoPayload::Bar );
assert_eq!( into_v(NoPayload::Baz), NoPayload::Baz );
// `NoPayload_ESI` was generated by the `Structural` derive macro for `NoPayload`,
// aliasing its accessor trait impls.
fn into_v(this: impl NoPayload_ESI)->NoPayload {
let this = StrucWrapper(this);
this.into_struc()
}
#[derive(Debug,Structural,PartialEq)]
enum WithPayload<T>{
Foo(T),
Bar(T),
Baz(T),
}
#[derive(Debug,Structural,PartialEq)]
enum NoPayload{
Foo,
Bar,
Baz,
}
// This macro allows enums to only implement the `TryFromStructural` trait,
// delegating the `FromStructural` trait to it.
structural::z_impl_try_from_structural_for_enum!{
impl[F] TryFromStructural<F> for NoPayload
where[ F: NoPayload_SI, ]
{
type Error = EmptyTryFromError;
fn try_from_structural(this){
switch!{this;
Foo => Ok(NoPayload::Foo),
Bar => Ok(NoPayload::Bar),
Baz => Ok(NoPayload::Baz),
_ => Err(TryFromError::with_empty_error(this)),
}
}
}
FromStructural
where[ F: NoPayload_ESI, ]
}
Sourcepub fn try_into_struc<U>(self) -> Result<U, TryFromError<T, T::Error>>where
T: TryIntoStructural<U>,
pub fn try_into_struc<U>(self) -> Result<U, TryFromError<T, T::Error>>where
T: TryIntoStructural<U>,
Performs a fallible conversion into another structural type using TryIntoStructural
.
§Enum example
use structural::{
convert::{EmptyTryFromError, TryFromError},
for_examples::ResultLike,
Structural, StructuralExt, switch,
};
assert_eq!(
MoreCommand::Open(Door::Green).try_into_struc::<Command>(),
Ok(Command::Open(Door::Green)),
);
assert_eq!(
MoreCommand::Close(Door::Green).try_into_struc::<Command>(),
Ok(Command::Close(Door::Green)),
);
let lock_cmd = MoreCommand::Lock(Door::Green, Key(12345678));
assert_eq!(
lock_cmd.clone().try_into_struc::<Command>(),
Err(TryFromError::with_empty_error(lock_cmd)),
);
#[derive(Debug,Clone,Structural,PartialEq)]
enum Command{
Open(Door),
Close(Door),
}
#[derive(Debug,Clone,Structural,PartialEq)]
enum MoreCommand{
Open(Door),
Close(Door),
Lock(Door, Key),
}
// This macro implements FromStructural in terms of TryFromStructural,
structural::z_impl_try_from_structural_for_enum!{
impl[F] TryFromStructural<F> for Command
where[ F: Command_SI, ]
{
type Error = EmptyTryFromError;
fn try_from_structural(this){
switch! {this;
Open(door) => Ok(Self::Open(door)),
Close(door) => Ok(Self::Close(door)),
_ => Err(TryFromError::with_empty_error(this)),
}
}
}
// `Command_ESI` was generated by the `Structural` derive for `Command`
// aliasing its accessor trait impls,
// and requires `F` to only have the `Open`,and `Close` variants.
FromStructural
where[ F: Command_ESI, ]
}
Source§impl<T> StrucWrapper<T>
impl<T> StrucWrapper<T>
Sourcepub fn as_ref(&self) -> StrucWrapper<&T>
pub fn as_ref(&self) -> StrucWrapper<&T>
Turns a &StrucWrapper<T>
into a StrucWrapper<&T>
.
§Example
use structural::{Structural, StrucWrapper, fp, make_struct};
{
let this= StrucWrapper(Struct3{foo:Some(13), bar:21, baz:"34"});
with_struct3(this.as_ref());
// Because of the `.as_ref()`,`this` wasn't consumed
with_struct3(this.as_ref());
}
{
let this= StrucWrapper(make_struct!{foo:Some(13), bar:21, baz:"34", quax:false});
with_struct3(this.as_ref());
// Because of the `.as_ref()`,`this` wasn't consumed
with_struct3(this.as_ref());
}
// The `Struct3_SI` trait was declared for Struct3 by the Structural derive macro,
// aliasing its accessor trait impls
//
// Also, notice how this also requires `Copy`,even though Struct3 doesn't implement it?
// The call with Struct3 works because of the `.as_ref()`,
// since `&` always implements `Copy`.
fn with_struct3(this: StrucWrapper<impl Struct3_SI<u8, u16, &'static str> + Copy>){
assert_eq!( this.r(fp!(foo?)), Some(&13) );
assert_eq!( this.r(fp!(bar)), &21 );
assert_eq!( this.r(fp!(baz)), &"34" );
}
#[derive(Structural, Debug)]
// With this attribute, you can only access fields by shared reference.
#[struc(access="ref")]
pub struct Struct3<A, B, C> {
pub foo: Option<A>,
pub bar: B,
pub baz: C,
}
Sourcepub fn as_mut(&mut self) -> StrucWrapper<&mut T>
pub fn as_mut(&mut self) -> StrucWrapper<&mut T>
Turns a &mut StrucWrapper<T>
into a StrucWrapper<&mut T>
.
Turns a &StrucWrapper<T>
into a StrucWrapper<&T>
.
§Example
use structural::{Structural, StrucWrapper, fp, make_struct};
{
let mut this= StrucWrapper(Struct3{foo:Some(13), bar:21, baz:"34"});
with_struct3(this.as_mut());
// Because of the `.as_mut()`,`this` wasn't consumed
with_struct3(this.as_mut());
}
{
let mut this= StrucWrapper(make_struct!{foo:Some(13), bar:21, baz:"34", quax:false});
with_struct3(this.as_mut());
// Because of the `.as_mut()`,`this` wasn't consumed
with_struct3(this.as_mut());
}
// The `Struct3_SI` trait was declared for Struct3 by the Structural derive macro,
// aliasing its accessor trait impls
fn with_struct3(mut this: StrucWrapper<impl Struct3_SI<u8, u16, &'static str>>){
assert_eq!( this.r(fp!(foo?)), Some(&13) );
assert_eq!( this.r(fp!(bar)), &21 );
assert_eq!( this.r(fp!(baz)), &"34" );
assert_eq!( this.m(fp!(foo?)), Some(&mut 13) );
assert_eq!( this.m(fp!(bar)), &mut 21 );
assert_eq!( this.m(fp!(baz)), &mut "34" );
}
#[derive(Structural, Debug)]
// With this attribute, you can only access fields by shared or mutable reference.
#[struc(access="mut")]
pub struct Struct3<A, B, C> {
pub foo: Option<A>,
pub bar: B,
pub baz: C,
}
Sourcepub fn map<F, U>(self, f: F) -> StrucWrapper<U>where
F: FnOnce(T) -> U,
pub fn map<F, U>(self, f: F) -> StrucWrapper<U>where
F: FnOnce(T) -> U,
Transforms the wrapped value with the func
function.
Sourcepub fn then<F, U>(self, f: F) -> StrucWrapper<U>where
F: FnOnce(Self) -> U,
pub fn then<F, U>(self, f: F) -> StrucWrapper<U>where
F: FnOnce(Self) -> U,
Calls func
with self
,rewrapping its return value in a StrucWrapper<U>
Source§impl<T: Clone> StrucWrapper<&T>
impl<T: Clone> StrucWrapper<&T>
Sourcepub fn cloned(self) -> StrucWrapper<T>
pub fn cloned(self) -> StrucWrapper<T>
Maps the wrapped reference into a clone.
Source§impl<T: Clone> StrucWrapper<&mut T>
impl<T: Clone> StrucWrapper<&mut T>
Sourcepub fn cloned(self) -> StrucWrapper<T>
pub fn cloned(self) -> StrucWrapper<T>
Maps the wrapped mutable reference into a clone.
Source§impl<'a, T> StrucWrapper<&'a T>
impl<'a, T> StrucWrapper<&'a T>
Sourcepub fn reref(self) -> &'a StrucWrapper<T>
pub fn reref(self) -> &'a StrucWrapper<T>
Turns a StrucWrapper<&T>
into a &StrucWrapper<T>
.
Note that this only works if T: Sized
,
which means that you can’t call this method on a StrucWrapper<&dyn Trait>
.
Source§impl<'a, T> StrucWrapper<&'a mut T>
impl<'a, T> StrucWrapper<&'a mut T>
Sourcepub fn remut(self) -> &'a mut StrucWrapper<T>
pub fn remut(self) -> &'a mut StrucWrapper<T>
Turns a StrucWrapper<&mut T>
into a &mut StrucWrapper<T>
.
Note that this only works if T: Sized
,
which means that you can’t call this method on a StrucWrapper<&mut dyn Trait>
.
Trait Implementations§
Source§impl<T: Clone> Clone for StrucWrapper<T>
impl<T: Clone> Clone for StrucWrapper<T>
Source§fn clone(&self) -> StrucWrapper<T>
fn clone(&self) -> StrucWrapper<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> ConstDefault for StrucWrapper<T>where
T: ConstDefault,
impl<T> ConstDefault for StrucWrapper<T>where
T: ConstDefault,
Source§impl<T: Debug> Debug for StrucWrapper<T>
impl<T: Debug> Debug for StrucWrapper<T>
Source§impl<T: Default> Default for StrucWrapper<T>
impl<T: Default> Default for StrucWrapper<T>
Source§fn default() -> StrucWrapper<T>
fn default() -> StrucWrapper<T>
Source§impl<T> DropFields for StrucWrapper<T>where
T: DropFields,
impl<T> DropFields for StrucWrapper<T>where
T: DropFields,
Source§unsafe fn drop_fields(&mut self, moved: MovedOutFields)
unsafe fn drop_fields(&mut self, moved: MovedOutFields)
Source§impl<T, NP, __Ty> FieldType<NP> for StrucWrapper<T>where
T: FieldType<NP, Ty = __Ty>,
impl<T, NP, __Ty> FieldType<NP> for StrucWrapper<T>where
T: FieldType<NP, Ty = __Ty>,
Source§impl<T, __From> FromStructural<__From> for StrucWrapper<T>where
T: FromStructural<__From>,
impl<T, __From> FromStructural<__From> for StrucWrapper<T>where
T: FromStructural<__From>,
Source§fn from_structural(from: __From) -> Self
fn from_structural(from: __From) -> Self
Source§impl<T, __F, __Ty> GetField<__F> for StrucWrapper<T>where
T: GetField<__F, Ty = __Ty>,
impl<T, __F, __Ty> GetField<__F> for StrucWrapper<T>where
T: GetField<__F, Ty = __Ty>,
Source§fn get_field_(&self, fname: __F) -> &__Ty
fn get_field_(&self, fname: __F) -> &__Ty
FieldName
field by reference.Source§impl<T, __F, __Ty> GetFieldMut<__F> for StrucWrapper<T>
impl<T, __F, __Ty> GetFieldMut<__F> for StrucWrapper<T>
Source§fn get_field_mut_(&mut self, fname: __F) -> &mut __Ty
fn get_field_mut_(&mut self, fname: __F) -> &mut __Ty
FieldName
field by mutable reference.Source§unsafe fn get_field_raw_mut(this: *mut (), fname: __F) -> *mut __Tywhere
Self: Sized,
unsafe fn get_field_raw_mut(this: *mut (), fname: __F) -> *mut __Tywhere
Self: Sized,
Source§fn get_field_raw_mut_fn(&self) -> GetFieldRawMutFn<__F, __Ty>
fn get_field_raw_mut_fn(&self) -> GetFieldRawMutFn<__F, __Ty>
get_field_raw_mut
associated function as a function pointer.Source§impl<T, __V, __F, __Ty> GetVariantField<TStr<__V>, __F> for StrucWrapper<T>where
T: GetVariantField<TStr<__V>, __F, Ty = __Ty>,
impl<T, __V, __F, __Ty> GetVariantField<TStr<__V>, __F> for StrucWrapper<T>where
T: GetVariantField<TStr<__V>, __F, Ty = __Ty>,
Source§fn get_vfield_(
&self,
vname: TStr<__V>,
fname: __F,
) -> Option<&GetVariantFieldType<T, TStr<__V>, __F>>
fn get_vfield_( &self, vname: TStr<__V>, fname: __F, ) -> Option<&GetVariantFieldType<T, TStr<__V>, __F>>
F
field in the V
variant by reference.Source§unsafe fn get_vfield_unchecked(&self, variant: V, field: F) -> &Self::Ty
unsafe fn get_vfield_unchecked(&self, variant: V, field: F) -> &Self::Ty
F
field in the V
variant by reference,
without checking that the enum is currently the V
variant. Read moreSource§impl<T, __V, __F, __Ty> GetVariantFieldMut<TStr<__V>, __F> for StrucWrapper<T>
impl<T, __V, __F, __Ty> GetVariantFieldMut<TStr<__V>, __F> for StrucWrapper<T>
Source§fn get_vfield_mut_(&mut self, vname: TStr<__V>, fname: __F) -> Option<&mut __Ty>
fn get_vfield_mut_(&mut self, vname: TStr<__V>, fname: __F) -> Option<&mut __Ty>
F
field in the V
variant by mutable reference.Source§unsafe fn get_vfield_raw_mut_(
this: *mut (),
vname: TStr<__V>,
fname: __F,
) -> Option<NonNull<__Ty>>where
Self: Sized,
unsafe fn get_vfield_raw_mut_(
this: *mut (),
vname: TStr<__V>,
fname: __F,
) -> Option<NonNull<__Ty>>where
Self: Sized,
Source§fn get_vfield_raw_mut_unchecked_fn(&self) -> GetFieldRawMutFn<__F, __Ty>
fn get_vfield_raw_mut_unchecked_fn(&self) -> GetFieldRawMutFn<__F, __Ty>
get_vfield_raw_mut_unchecked
method. Read moreSource§fn get_vfield_raw_mut_fn(&self) -> GetVFieldRawMutFn<TStr<__V>, __F, __Ty>
fn get_vfield_raw_mut_fn(&self) -> GetVFieldRawMutFn<TStr<__V>, __F, __Ty>
get_vfield_raw_mut_
method. Read moreSource§unsafe fn get_vfield_mut_unchecked(
&mut self,
variant: V,
field: F,
) -> &mut Self::Ty
unsafe fn get_vfield_mut_unchecked( &mut self, variant: V, field: F, ) -> &mut Self::Ty
F
field in the V
variant by mutable reference,
without checking that the enum is currently the V
variant. Read moreSource§unsafe fn get_vfield_raw_mut_unchecked(ptr: *mut (), field: F) -> *mut Self::Tywhere
Self: Sized,
V: ConstDefault,
unsafe fn get_vfield_raw_mut_unchecked(ptr: *mut (), field: F) -> *mut Self::Tywhere
Self: Sized,
V: ConstDefault,
F
field in the V
variant by raw pointer,
without checking that the enum is currently the V
variant. Read moreSource§impl<T: Hash> Hash for StrucWrapper<T>
impl<T: Hash> Hash for StrucWrapper<T>
Source§impl<F, T> Index<F> for StrucWrapper<T>where
T: GetField<F>,
Gets a reference to a non-nested struct field
impl<F, T> Index<F> for StrucWrapper<T>where
T: GetField<F>,
Gets a reference to a non-nested struct field
§Example
use structural::{StrucWrapper, fp};
use structural::structural_aliases::Array4;
assertions(["hello","world","foo","bar"]);
assertions(["hello","world","foo","bar","baz"]);
assertions(("hello","world","foo","bar"));
assertions(("hello","world","foo","bar","baz"));
assertions(("hello","world","foo","bar","baz","qux"));
fn assertions(this: impl Array4<&'static str> ){
let this=StrucWrapper(this);
assert_eq!( this[fp!(1)], "world" );
assert_eq!( this[fp!(2)], "foo" );
assert_eq!( this[fp!(3)], "bar" );
}
Source§impl<F, T> IndexMut<F> for StrucWrapper<T>where
T: GetFieldMut<F>,
Gets a mutable reference to a non-nested struct field
impl<F, T> IndexMut<F> for StrucWrapper<T>where
T: GetFieldMut<F>,
Gets a mutable reference to a non-nested struct field
§Example
use structural::{StructuralExt, StrucWrapper, fp, make_struct};
use structural::for_examples::{Struct3, Struct3_SI};
let mut this=Struct3{ foo:Some(33), bar:55, baz:77 };
let mut anon=make_struct!{ foo:Some(0), bar:0, baz:0, extra:"extra" };
fn mutator(this:&mut impl Struct3_SI<u32,u32,u32>){
let mut this=StrucWrapper(this);
this[fp!(bar)]+=20;
this[fp!(baz)]+=30;
}
mutator(&mut this);
mutator(&mut anon);
assert_eq!( this.cloned_fields(fp!(foo, bar, baz)), (Some(33), 75, 107) );
assert_eq!( anon.cloned_fields(fp!(foo, bar, baz, extra)), (Some(0), 20, 30, "extra") );
Source§impl<T, __F, __Ty> IntoField<__F> for StrucWrapper<T>where
T: IntoField<__F, Ty = __Ty>,
impl<T, __F, __Ty> IntoField<__F> for StrucWrapper<T>where
T: IntoField<__F, Ty = __Ty>,
Source§fn into_field_(self, fname: __F) -> __Ty
fn into_field_(self, fname: __F) -> __Ty
Source§unsafe fn move_out_field_(
&mut self,
fname: __F,
moved: &mut MovedOutFields,
) -> __Ty
unsafe fn move_out_field_( &mut self, fname: __F, moved: &mut MovedOutFields, ) -> __Ty
Source§impl<T, __V, __F, __Ty> IntoVariantField<TStr<__V>, __F> for StrucWrapper<T>where
T: IntoVariantField<TStr<__V>, __F, Ty = __Ty>,
impl<T, __V, __F, __Ty> IntoVariantField<TStr<__V>, __F> for StrucWrapper<T>where
T: IntoVariantField<TStr<__V>, __F, Ty = __Ty>,
Source§fn into_vfield_(self, vname: TStr<__V>, fname: __F) -> Option<__Ty>
fn into_vfield_(self, vname: TStr<__V>, fname: __F) -> Option<__Ty>
F
field in the V
variant by value.Source§unsafe fn move_out_vfield_(
&mut self,
vname: TStr<__V>,
fname: __F,
moved: &mut MovedOutFields,
) -> Option<__Ty>
unsafe fn move_out_vfield_( &mut self, vname: TStr<__V>, fname: __F, moved: &mut MovedOutFields, ) -> Option<__Ty>
Source§unsafe fn into_vfield_unchecked_(
self,
variant_name: V,
field_name: F,
) -> Self::Tywhere
Self: Sized,
unsafe fn into_vfield_unchecked_(
self,
variant_name: V,
field_name: F,
) -> Self::Tywhere
Self: Sized,
Source§unsafe fn move_out_vfield_unchecked_(
&mut self,
variant_name: V,
field_name: F,
moved_fields: &mut MovedOutFields,
) -> Self::Ty
unsafe fn move_out_vfield_unchecked_( &mut self, variant_name: V, field_name: F, moved_fields: &mut MovedOutFields, ) -> Self::Ty
F
field in the V
variant by value,
without checking that the enum is currently the V
variant. Read moreSource§impl<T, __V> IsVariant<TStr<__V>> for StrucWrapper<T>
impl<T, __V> IsVariant<TStr<__V>> for StrucWrapper<T>
Source§fn is_variant_(&self, name: TStr<__V>) -> bool
fn is_variant_(&self, name: TStr<__V>) -> bool
V
stands for.Source§impl<T: Ord> Ord for StrucWrapper<T>
impl<T: Ord> Ord for StrucWrapper<T>
Source§fn cmp(&self, other: &StrucWrapper<T>) -> Ordering
fn cmp(&self, other: &StrucWrapper<T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for StrucWrapper<T>
impl<T: PartialEq> PartialEq for StrucWrapper<T>
Source§impl<T: PartialOrd> PartialOrd for StrucWrapper<T>
impl<T: PartialOrd> PartialOrd for StrucWrapper<T>
Source§impl<T, __From, __Err> TryFromStructural<__From> for StrucWrapper<T>where
T: TryFromStructural<__From, Error = __Err>,
impl<T, __From, __Err> TryFromStructural<__From> for StrucWrapper<T>where
T: TryFromStructural<__From, Error = __Err>,
Source§type Error = __Err
type Error = __Err
TryFromError
,
returned from try_into_structural
on conversion error.Source§fn try_from_structural(
from: __From,
) -> Result<Self, TryFromError<__From, __Err>>
fn try_from_structural( from: __From, ) -> Result<Self, TryFromError<__From, __Err>>
Source§impl<T> VariantCount for StrucWrapper<T>where
T: VariantCount,
impl<T> VariantCount for StrucWrapper<T>where
T: VariantCount,
impl<T: Copy> Copy for StrucWrapper<T>
impl<T: Eq> Eq for StrucWrapper<T>
impl<T> Structural for StrucWrapper<T>where
T: Structural,
impl<T> StructuralPartialEq for StrucWrapper<T>
Auto Trait Implementations§
impl<T> Freeze for StrucWrapper<T>where
T: Freeze,
impl<T> RefUnwindSafe for StrucWrapper<T>where
T: RefUnwindSafe,
impl<T> Send for StrucWrapper<T>where
T: Send,
impl<T> Sync for StrucWrapper<T>where
T: Sync,
impl<T> Unpin for StrucWrapper<T>where
T: Unpin,
impl<T> UnwindSafe for StrucWrapper<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<This> EnumExt for Thiswhere
This: ?Sized,
impl<This> EnumExt for Thiswhere
This: ?Sized,
Source§fn as_variant<V>(
&self,
vari: TStr<V>,
) -> Result<&VariantProxy<Self, TStr<V>>, &Self>
fn as_variant<V>( &self, vari: TStr<V>, ) -> Result<&VariantProxy<Self, TStr<V>>, &Self>
Source§fn as_mut_variant<V>(
&mut self,
vari: TStr<V>,
) -> Result<&mut VariantProxy<Self, TStr<V>>, &mut Self>
fn as_mut_variant<V>( &mut self, vari: TStr<V>, ) -> Result<&mut VariantProxy<Self, TStr<V>>, &mut Self>
Source§impl<This, T> IntoStructural<T> for Thiswhere
T: FromStructural<This>,
impl<This, T> IntoStructural<T> for Thiswhere
T: FromStructural<This>,
Source§fn into_structural(self) -> T
fn into_structural(self) -> T
Source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
Source§const T: PhantomData<fn() -> Self> = PhantomData
const T: PhantomData<fn() -> Self> = PhantomData
Type::T
to pass it in methods with _:VariantPhantom<T>
parameters. Read moreSource§const T_D: PhantomData<Self> = PhantomData
const T_D: PhantomData<Self> = PhantomData
Source§fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Selfwhere
Self: Sized,
fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Selfwhere
Self: Sized,
other
is the same type as self
.Source§fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Selfwhere
Self: Sized,
fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Selfwhere
Self: Sized,
other
is the same type as self
.Source§fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Selfwhere
Self: Sized,
fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Selfwhere
Self: Sized,
other
is the same type as self
.Source§fn ty_(&self) -> PhantomData<fn() -> Self>
fn ty_(&self) -> PhantomData<fn() -> Self>
Source§fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
Source§fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
Source§fn eq_id(&self, other: &Self) -> bool
fn eq_id(&self, other: &Self) -> bool
Source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
Source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read moreSource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.Source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
Source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
Source§fn into_<T>(self, _: PhantomData<fn() -> T>) -> Twhere
Self: Into<T>,
fn into_<T>(self, _: PhantomData<fn() -> T>) -> Twhere
Self: Into<T>,
Source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
.as_ref_::<_>()
syntax. Read moreSource§impl<T> StructuralExt for Twhere
T: ?Sized,
impl<T> StructuralExt for Twhere
T: ?Sized,
Source§fn field_<'a, P>(
&'a self,
path: P,
) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
fn field_<'a, P>( &'a self, path: P, ) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
path
. Read moreSource§fn fields<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, Self>where
P: RevGetMultiField<'a, Self>,
fn fields<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, Self>where
P: RevGetMultiField<'a, Self>,
path
. Read moreSource§fn cloned_fields<'a, P>(
&'a self,
path: P,
) -> ClonedOut<RevGetMultiFieldOut<'a, P, Self>>
fn cloned_fields<'a, P>( &'a self, path: P, ) -> ClonedOut<RevGetMultiFieldOut<'a, P, Self>>
path
. Read moreSource§fn field_mut<'a, P>(
&'a mut self,
path: P,
) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
fn field_mut<'a, P>( &'a mut self, path: P, ) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
path
. Read moreSource§fn fields_mut<'a, P>(
&'a mut self,
path: P,
) -> RevGetMultiFieldMutOut<'a, P, Self>where
P: RevGetMultiFieldMut<'a, Self>,
fn fields_mut<'a, P>(
&'a mut self,
path: P,
) -> RevGetMultiFieldMutOut<'a, P, Self>where
P: RevGetMultiFieldMut<'a, Self>,
path
. Read moreSource§fn into_field<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
fn into_field<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
path
. Read moreSource§fn into_fields<P>(self, path: P) -> RevIntoMultiFieldOut<P, Self>where
P: RevIntoMultiField<Self>,
Self: Sized,
fn into_fields<P>(self, path: P) -> RevIntoMultiFieldOut<P, Self>where
P: RevIntoMultiField<Self>,
Self: Sized,
self
into multiple fields by value. Read moreSource§fn is_variant<P>(&self, _path: P) -> bool
fn is_variant<P>(&self, _path: P) -> bool
Source§fn into_struc<U>(self) -> Uwhere
Self: IntoStructural<U>,
fn into_struc<U>(self) -> Uwhere
Self: IntoStructural<U>,
IntoStructural
. Read moreSource§fn try_into_struc<U>(self) -> Result<U, TryFromError<Self, Self::Error>>where
Self: TryIntoStructural<U>,
fn try_into_struc<U>(self) -> Result<U, TryFromError<Self, Self::Error>>where
Self: TryIntoStructural<U>,
TryIntoStructural
. Read moreSource§impl<This, T> TryIntoStructural<T> for Thiswhere
T: TryFromStructural<This>,
impl<This, T> TryIntoStructural<T> for Thiswhere
T: TryFromStructural<This>,
Source§type Error = <T as TryFromStructural<This>>::Error
type Error = <T as TryFromStructural<This>>::Error
TryFromError
,
returned from try_into_structural
on conversion error.