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§
Trait Implementations§
Auto Trait Implementations§
impl<H, E> Freeze for Owned<H, E>
impl<H, E> RefUnwindSafe for Owned<H, E>where
H: RefUnwindSafe,
E: ?Sized,
impl<H, E> Send for Owned<H, E>
impl<H, E> Sync for Owned<H, E>
impl<H, E> Unpin for Owned<H, E>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more