Struct Post

Source
pub struct Post { /* private fields */ }
Expand description

A Resource to send command or future.

This resource provides clients funtionalities to send commands or futures in their systems. This resource also provides interior mutability, so that clients can request the resource with ResRead.

Implementations§

Source§

impl Post

Source

pub fn send_command<F>(&self, f: F)
where F: FnOnce(Ecs<'_>) -> DynResult<()> + Send + 'static,

Sends the given command to ECS scheduler.

Commands are executed on the main worker, but the command is not executed at the time of sending. ECS scheduler runs all buffered commands at the end of current cycle and before the next cycle.

§Examples
use my_ecs::prelude::*;
use std::sync::mpsc;

let (tx, rx) = mpsc::channel();

// A system sending a command.
let tx_a = tx.clone();
let sys_a = move |rr: ResRead<Post>| {
    tx_a.send("sys_a").unwrap();
    let tx_aa = tx_a.clone();
    rr.send_command(move |ecs| {
        tx_aa.send("cmd_a").unwrap();
        Ok(())
    });
};

// A system sending a command.
let tx_b = tx.clone();
let sys_b = move |rr: ResRead<Post>| {
    tx_b.send("sys_b").unwrap();
    let tx_bb = tx_b.clone();
    rr.send_command(move |ecs| {
        tx_bb.send("cmd_b").unwrap();
        Ok(())
    });
};

Ecs::default(WorkerPool::with_len(2), [2])
    .add_systems((sys_a, sys_b))
    .step();

// No data dependency between `sys_a` and `sys_b`, so they can be run
// simultaneously.
assert!(matches!(rx.try_recv(), Ok("sys_a") | Ok("sys_b")));
assert!(matches!(rx.try_recv(), Ok("sys_a") | Ok("sys_b")));
assert!(matches!(rx.try_recv(), Ok("cmd_a") | Ok("cmd_b")));
assert!(matches!(rx.try_recv(), Ok("cmd_a") | Ok("cmd_b")));
Source

pub fn send_future<F, R>(&self, future: F)
where F: Future<Output = R> + Send + 'static, R: Command,

Sends the given future to ECS scheduler.

The future is guaranteed to be polled more than or just once in the current cycle.

§Examples
use my_ecs::prelude::*;
use std::{time::Duration, sync::{Arc, Mutex}};

let state = Arc::new(Mutex::new(0));

let c_state = Arc::clone(&state);
let foo = async move {
    *c_state.lock().unwrap() = 1;
    async_io::Timer::after(Duration::from_millis(10)).await;
    *c_state.lock().unwrap() = 2;
};

Ecs::default(WorkerPool::new(), [])
    .add_once_system(move |rr: ResRead<Post>| {
        rr.send_future(foo);
    })
    .run(|_| {});

assert_eq!(*state.lock().unwrap(), 2);
Source

pub fn request_lock<'buf, Req>(&self) -> RequestLockFuture<'buf, Req>
where Req: Request,

Source

pub fn change_entity(&self, eid: EntityId) -> EntityMoveCommandBuilder<'_>

Trait Implementations§

Auto Trait Implementations§

§

impl !Freeze for Post

§

impl RefUnwindSafe for Post

§

impl Send for Post

§

impl Sync for Post

§

impl Unpin for Post

§

impl UnwindSafe for Post

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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.