[][src]Trait structural::field::multi_fields::RevGetMultiFieldMut

pub trait RevGetMultiFieldMut<'a, This: ?Sized + 'a>: RevGetMultiFieldMutImpl<'a, This> {
    type FieldsMut: 'a;
    type FieldsRawMut: 'a;
    fn rev_get_multi_field_mut(self, this: &'a mut This) -> Self::FieldsMut;
unsafe fn rev_get_multi_field_raw_mut(
        self,
        this: *mut This
    ) -> Self::FieldsRawMut; }

Gets mutable references to multiple fields from This, usually a tuple of Option<&mut _>s and &mut _s.

This trait has a blanket implementation for the RevGetMultiFieldMutImpl trait, implementing that trait is necessary to be able to use this trait.

This is used by the StructuralExt::fields_mut method.

There's also the RevGetMultiFieldMutOut type alias to get this trait's FieldsMut associated type.

Example

This demonstrates using RevGetMultiFieldMut with structs.

use structural::{StructuralExt, FP, fp};
use structural::field::RevGetMultiFieldMut;
use structural::for_examples::{Tuple2, Tuple3};

use std::cmp::Ordering;

fn access_fields<'a,T,O0,O1>(this: &'a mut T)->(&'a mut O0,&'a mut O1)
where
    // The `FieldsMut= (&'a mut O0,&'a mut O1)` constraint ensures that
    // the return type is a pair.
    FP!(0,1): RevGetMultiFieldMut<'a,T,FieldsMut= (&'a mut O0,&'a mut O1)>
{
    this.fields_mut(fp!(0,1))
}

let mut tup2 = Tuple2(Some(8), Ordering::Less);
let mut tup3 = Tuple3(Some("hello"), Some(true), 34);

assert_eq!( access_fields(&mut tup2), (&mut Some(8), &mut Ordering::Less) );
assert_eq!( access_fields(&mut tup3), (&mut Some("hello"), &mut Some(true)) );

Example

This demonstrates how you can use RevGetMultiFieldMut with enums.

use structural::{StructuralExt, field_path_aliases};
use structural::field::{RevGetMultiFieldMut,RevGetMultiFieldMutOut};
use structural::for_examples::{Bomb, WithBoom};

use std::cmp::Ordering;

// Using the `field_path_aliases` macro is required to declare the `ThePath` constant,
// since it's unsafe to construct paths for borrowing multiple fields mutably
// (the macro ensures that they alias doesn't refer to the same field multiple times).
field_path_aliases!{
    ThePath=(::Boom=>a,b),
}
fn access_fields<'a,T,O0,O1>(this: &'a mut T)->Option<(&'a mut O0,&'a mut O1)>
where
    // The `FieldsMut= Option<(&'a mut O0,&'a mut O1)>` constraint ensures that
    // the return type is an optional pair.
    ThePath: RevGetMultiFieldMut<'a,T,FieldsMut= Option<(&'a mut O0,&'a mut O1)>>
{
    this.fields_mut(ThePath)
}

fn main(){
    let mut with_0 = WithBoom::Nope;
    let mut with_1 = WithBoom::Boom{a:"hi", b: &[0,1,2]};
    assert_eq!( access_fields(&mut with_0), None );
    assert_eq!( access_fields(&mut with_1), Some((&mut "hi", &mut &[0,1,2][..])) );
    
    let mut bomb_0 = Bomb::Nope;
    let mut bomb_1 = Bomb::Boom{a:"hello", b: &[5,8,13]};
    assert_eq!( access_fields(&mut bomb_0), None );
    assert_eq!( access_fields(&mut bomb_1), Some((&mut "hello", &mut &[5,8,13][..])) );
}

Associated Types

type FieldsMut: 'a

This is usually a tuple of Option<&mut _>s and &mut _s.

type FieldsRawMut: 'a

This is usually a tuple of Option<*mut _>s and *mut _s.

Loading content...

Required methods

fn rev_get_multi_field_mut(self, this: &'a mut This) -> Self::FieldsMut

Gets mutable references to multiple fields from this, usually a tuple of Option<&mut _>s and &mut _s.

unsafe fn rev_get_multi_field_raw_mut(
    self,
    this: *mut This
) -> Self::FieldsRawMut

Gets raw pointers to multiple fields from this, usually a tuple of Option<*mut _>s and *mut _s.

Safety

this must point to a valid instance of This,which lives for the 'a lifetime.

Loading content...

Implementors

impl<'a, This: ?Sized, Path> RevGetMultiFieldMut<'a, This> for Path where
    This: 'a,
    Path: RevGetMultiFieldMutImpl<'a, This>, 
[src]

Loading content...