Struct Cor

Source
pub struct Cor<RETURN: 'static, RECEIVE: 'static> { /* private fields */ }
Expand description

The Cor implements a PythonicGenerator-like Coroutine.

§Arguments

  • RETURN - The generic type of returned data
  • RECEIVE - The generic type of received data

§Remarks

It could be sync or async up to your usages, and it could use yield_from to send a value to another Cor object and get the response, and use yield_ref()/yield_none() to return my response to the callee of mine.

NOTE: Beware the deadlock if it’s sync(waiting for each other), except the entry point.

Implementations§

Source§

impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN, RECEIVE>

Source

pub fn new( effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static, ) -> Cor<RETURN, RECEIVE>

Generate a new Cor with the given FnMut function for the execution of this Cor.

§Arguments
  • effect - The given FnMut, the execution code of Cor.
Source

pub fn new_with_mutex( effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static, ) -> Arc<Mutex<Cor<RETURN, RECEIVE>>>

Generate a new Arc<Mutex<Cor<RETURN, RECEIVE>>> with the given FnMut function for the execution of this Cor.

§Arguments
  • effect - The given FnMut, the execution code of Cor.
Source

pub fn yield_from<RETURNTARGET: Send + Sync + 'static, RECEIVETARGET: Send + Sync + 'static>( this: Arc<Mutex<Cor<RETURN, RECEIVE>>>, target: Arc<Mutex<Cor<RETURNTARGET, RECEIVETARGET>>>, sent_to_inside: Option<RECEIVETARGET>, ) -> Option<RETURNTARGET>

Make this sends a given Option<RECEIVETARGET> to target, and this method returns the Option<RETURNTARGET> response from target.

§Arguments
  • this - The sender when sending sent_to_inside to target.
  • target - The receiver of value sent_to_inside sent by this.
  • sent_to_inside - The value sent by this and received by target.
§Remarks

This method is implemented according to some coroutine/generator implementations, such as Python, Lua, ECMASript…etc.

Source

pub fn yield_none(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>) -> Option<RECEIVE>

Make this returns a given None::<RETURN> to its callee Cor, and this method returns the Option<RECEIVE> value given from outside.

§Arguments
  • this - The sender when sending given_to_outside to callee Cor.
§Remarks

This method is implemented according to some coroutine/generator implementations, such as Python, Lua, ECMASript…etc.

Source

pub fn yield_ref( this: Arc<Mutex<Cor<RETURN, RECEIVE>>>, given_to_outside: Option<RETURN>, ) -> Option<RECEIVE>

Make this returns a given Option<RETURN> given_to_outside to its callee Cor, and this method returns the Option<RECEIVE> value given from outside.

§Arguments
  • this - The sender when sending given_to_outside to callee Cor.
  • given_to_outside - The value sent by this and received by target.
§Remarks

This method is implemented according to some coroutine/generator implementations, such as Python, Lua, ECMASript…etc.

Source

pub fn start(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>)

Start this Cor.

§Arguments
  • this - The target Cor to start.

NOTE: Beware the deadlock if it’s sync(waiting for each other), except the entry point.

Source

pub fn set_async(&mut self, is_async: bool)

Setup async or not. Default async: true

§Arguments
  • async - async when true, otherwise sync.

NOTE: Beware the deadlock if it’s sync(waiting for each other), except the entry point.

Source

pub fn is_started(&self) -> bool

Did this Cor start? Return true when it did started (no matter it has stopped or not)

Source

pub fn is_alive(&self) -> bool

Is this Cor alive? Return true when it has started and not stopped yet.

Source

pub fn stop(&mut self)

Stop Cor. This will make self.is_alive() returns false, and all yield_from() from this Cor as target will return None::<RETURN>. (Because it has stopped :P, that’s reasonable)

Trait Implementations§

Source§

impl<RETURN: Clone + 'static, RECEIVE: Clone + 'static> Clone for Cor<RETURN, RECEIVE>

Source§

fn clone(&self) -> Cor<RETURN, RECEIVE>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<RETURN, RECEIVE> Freeze for Cor<RETURN, RECEIVE>

§

impl<RETURN, RECEIVE> RefUnwindSafe for Cor<RETURN, RECEIVE>

§

impl<RETURN, RECEIVE> Send for Cor<RETURN, RECEIVE>
where RECEIVE: Send, RETURN: Send,

§

impl<RETURN, RECEIVE> Sync for Cor<RETURN, RECEIVE>
where RECEIVE: Send, RETURN: Send,

§

impl<RETURN, RECEIVE> Unpin for Cor<RETURN, RECEIVE>

§

impl<RETURN, RECEIVE> UnwindSafe for Cor<RETURN, RECEIVE>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.