Skip to main content

Owned

Struct Owned 

Source
pub struct Owned<H, E: ?Sized> { /* private fields */ }
Expand description

Reference-to-owned adapter via ToOwned. Wraps a Handler<E::Owned> and implements Handler<&E> — the event is converted via to_owned() before dispatch.

More general than Cloned: handles &str → String, &[u8] → Vec<u8>, and any other ToOwned impl where the owned type differs from the reference target. For T: Clone, ToOwned is blanket-implemented with Owned = T, so this adapter also works as a drop-in replacement for Cloned in those cases.

E must be named explicitly because the ToOwned mapping is not invertible — given Handler<String>, the compiler cannot infer that E = str. Use Owned::new with turbofish when needed. For simple Clone types where E = E::Owned, prefer Cloned.

§Examples

use nexus_rt::{WorldBuilder, ResMut, IntoHandler, Handler, Owned, Resource};

#[derive(Resource)]
struct Buffer(String);

fn process(mut buf: ResMut<Buffer>, event: String) {
    buf.0.push_str(&event);
}

let mut builder = WorldBuilder::new();
builder.register(Buffer(String::new()));
let mut world = builder.build();

let h = process.into_handler(world.registry());
let mut adapted = Owned::<_, str>::new(h);
adapted.run(&mut world, "hello");
assert_eq!(world.resource::<Buffer>().0.as_str(), "hello");

Implementations§

Source§

impl<H, E: ?Sized> Owned<H, E>

Source

pub fn new(handler: H) -> Self

Create a new Owned adapter.

When E cannot be inferred, use turbofish: Owned::<_, str>::new(handler).

Trait Implementations§

Source§

impl<'e, E, H> Handler<&'e E> for Owned<H, E>
where E: ToOwned + 'e + ?Sized, H: Handler<E::Owned> + Send,

Source§

fn run(&mut self, world: &mut World, event: &'e E)

Run this handler with the given event.
Source§

fn name(&self) -> &'static str

Returns the handler’s name. Read more

Auto Trait Implementations§

§

impl<H, E> Freeze for Owned<H, E>
where H: Freeze, E: ?Sized,

§

impl<H, E> RefUnwindSafe for Owned<H, E>
where H: RefUnwindSafe, E: ?Sized,

§

impl<H, E> Send for Owned<H, E>
where H: Send, E: ?Sized,

§

impl<H, E> Sync for Owned<H, E>
where H: Sync, E: ?Sized,

§

impl<H, E> Unpin for Owned<H, E>
where H: Unpin, E: ?Sized,

§

impl<H, E> UnsafeUnpin for Owned<H, E>
where H: UnsafeUnpin, E: ?Sized,

§

impl<H, E> UnwindSafe for Owned<H, E>
where H: UnwindSafe, E: ?Sized,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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<E, H> IntoHandler<E, Resolved> for H
where H: Handler<E> + 'static,

Source§

type Handler = H

The concrete handler type produced.
Source§

fn into_handler(self, _registry: &Registry) -> H

Convert this function into a handler, resolving parameters from the registry.
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.