pub trait RevGetMultiField<'a, This: ?Sized + 'a>: RevGetMultiFieldImpl<'a, This> {
type Fields: 'a;
// Required method
fn rev_get_multi_field(self, this: &'a This) -> Self::Fields;
}
Expand description
Gets references to multiple fields from This
,
usually a tuple of Option<&_>
s and &_
s
This trait has a blanket implementation for the RevGetMultiFieldImpl
trait,
implementing that trait is necessary to be able to use this trait.
This is used by the
StructuralExt::fields
,
and StructuralExt::cloned_fields
methods.
There’s also the RevGetMultiFieldOut
type alias to get this trait’s
Fields
associated type.
§Example
This demonstrates how you can use RevGetMultiField
with structs.
use structural::{StructuralExt, FP, fp};
use structural::field::RevGetMultiField;
use structural::for_examples::{Tuple2, Tuple3};
use std::cmp::Ordering;
fn access_fields<'a,T,O0,O1>(this: &'a T)->(&'a O0,&'a O1)
where
// The `Fields= (&'a O0,&'a O1)` constraint ensures that the return type is a pair.
FP!(0,1): RevGetMultiField<'a,T,Fields= (&'a O0,&'a O1)>
{
this.fields(fp!(0,1))
}
let tup2 = Tuple2(Some(8), Ordering::Less);
let tup3 = Tuple3(Some("hello"), Some(true), 34);
assert_eq!( access_fields(&tup2), (&Some(8), &Ordering::Less) );
assert_eq!( access_fields(&tup3), (&Some("hello"), &Some(true)) );
§Example
This demonstrates how you can use RevGetMultiField
with enums.
use structural::{StructuralExt, FP};
use structural::path::IntoAliasingOut;
use structural::field::{RevGetMultiField,RevGetMultiFieldOut};
use structural::for_examples::{Bomb, WithBoom};
use std::cmp::Ordering;
// Used `IntoAliasingOut` here to make the path safely constructible,
// since cosntructing a `NestedFieldPathSet<_,_,UniquePaths>`
// (what `FP!(::Boom=>a,b)` desugars into) requires unsafe code to construct,
// while `NestedFieldPathSet<_,_,AliasedPaths>`
// (what `IntoAliasingOut` turns `FP!(::Boom=>a,b)` into)
// does not require unsafe code to construct
//
// While this is safely constructible,it can't be used to do multiple mutable borrows,
// you can use `field_path_aliases` to avoid writing `unsafe` code in that case.
type ThePath=IntoAliasingOut<FP!(::Boom=>a,b)>;
fn access_fields<'a,T,O0,O1>(this: &'a T)->Option<(&'a O0,&'a O1)>
where
// The `Fields= Option<(&'a O0,&'a O1)>` constraint ensures that
// the return type is an optional pair.
ThePath: RevGetMultiField<'a,T,Fields= Option<(&'a O0,&'a O1)>>
{
this.fields(ThePath::NEW)
}
fn main(){
let with_0 = WithBoom::Nope;
let with_1 = WithBoom::Boom{a:"hi", b: &[0,1,2]};
assert_eq!( access_fields(&with_0), None );
assert_eq!( access_fields(&with_1), Some((&"hi", &&[0,1,2][..])) );
let bomb_0 = Bomb::Nope;
let bomb_1 = Bomb::Boom{a:"hello", b: &[5,8,13]};
assert_eq!( access_fields(&bomb_0), None );
assert_eq!( access_fields(&bomb_1), Some((&"hello", &&[5,8,13][..])) );
}
Required Associated Types§
Required Methods§
Sourcefn rev_get_multi_field(self, this: &'a This) -> Self::Fields
fn rev_get_multi_field(self, this: &'a This) -> Self::Fields
Gets references to multiple fields from this
,
usually a tuple of Option<&_>
s and &_
s.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.