Skip to main content

EnumerableMut

Trait EnumerableMut 

Source
pub trait EnumerableMut<'a, EnumMut> {
    // Required method
    fn as_enum_mut(&'a mut self) -> EnumMut;
}
Expand description

A type that can lend itself mutably to the borrowing enum of its sealed trait.

The counterpart of EnumerableRef, and reached from a &mut S the same way:

use closed_trait::{enumerate, sealed};

pub struct Square { pub side: i32 }
pub struct Circle { pub radius: i32 }

#[enumerate]
#[sealed(Square, Circle)]
pub trait Shape {
    fn grow(&mut self);
}

impl Shape for Square { fn grow(&mut self) { self.side += 1; } }
impl Shape for Circle { fn grow(&mut self) { self.radius += 1; } }

fn grow_twice<S: Shape>(shape: &mut S) {
    match shape.as_enum_mut() {
        AnyShapeMut::Square(s) => { s.grow(); s.grow(); }
        AnyShapeMut::Circle(c) => { c.grow(); c.grow(); }
    }
}

fn main() {
    let mut square = Square { side: 1 };
    grow_twice(&mut square);
    assert_eq!(square.side, 3);
}

Unlike the shared enum this one is neither Clone nor Copy, a unique reference being neither.

Required Methods§

Source

fn as_enum_mut(&'a mut self) -> EnumMut

Wraps &mut self in the variant of EnumMut that holds this type.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§