Struct stakker::ActorOwnAnon

source ·
pub struct ActorOwnAnon(_);
Expand description

An owning ref-counting reference to an anonymous actor

The purpose of this is to allow owning any one of a class of other actors without knowing the exact type. The only operation this supports is dropping an owning reference to an actor when this value is dropped. It can be used in combination with a Fwd instance to support plugging a variety of different actors into a standard interface, without needing traits. As an alternative, see actor_of_trait!.

Example, using ActorOwn::anon to create the anonymous reference:

struct Cat;
impl Cat {
    fn init(_: CX![]) -> Option<Self> {
        Some(Self)
    }
    fn sound(&mut self, _: CX![]) {
        println!("Miaow");
    }
}

struct Dog;
impl Dog {
    fn init(_: CX![]) -> Option<Self> {
        Some(Self)
    }
    fn sound(&mut self, _: CX![]) {
        println!("Woof");
    }
}

// This function doesn't know whether it's getting a cat or a dog,
// but it can still call it and drop it when it has finished
pub fn call_and_drop(sound: Fwd<()>, own: ActorOwnAnon) {
    fwd!([sound]);
}

let mut stakker = Stakker::new(Instant::now());
let s = &mut stakker;

let cat = actor!(s, Cat::init(), ret_nop!());
call_and_drop(fwd_to!([cat], sound() as ()), cat.anon());

let dog = actor!(s, Dog::init(), ret_nop!());
call_and_drop(fwd_to!([dog], sound() as ()), dog.anon());

s.run(Instant::now(), false);

Implementations§

source§

impl ActorOwnAnon

source

pub fn new<T: 'static>(actorown: ActorOwn<T>) -> Self

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.