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 dataRECEIVE
- 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>
impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN, RECEIVE>
Sourcepub fn new(
effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static,
) -> Cor<RETURN, RECEIVE>
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 givenFnMut
, the execution code ofCor
.
Sourcepub fn new_with_mutex(
effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static,
) -> Arc<Mutex<Cor<RETURN, RECEIVE>>>
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 givenFnMut
, the execution code ofCor
.
Sourcepub 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>
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 sendingsent_to_inside
totarget
.target
- The receiver of valuesent_to_inside
sent bythis
.sent_to_inside
- The value sent bythis
and received bytarget
.
§Remarks
This method is implemented according to some coroutine/generator implementations,
such as Python
, Lua
, ECMASript
…etc.
Sourcepub fn yield_none(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>) -> Option<RECEIVE>
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 sendinggiven_to_outside
to calleeCor
.
§Remarks
This method is implemented according to some coroutine/generator implementations,
such as Python
, Lua
, ECMASript
…etc.
Sourcepub fn yield_ref(
this: Arc<Mutex<Cor<RETURN, RECEIVE>>>,
given_to_outside: Option<RETURN>,
) -> Option<RECEIVE>
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 sendinggiven_to_outside
to calleeCor
.given_to_outside
- The value sent bythis
and received bytarget
.
§Remarks
This method is implemented according to some coroutine/generator implementations,
such as Python
, Lua
, ECMASript
…etc.
Sourcepub fn start(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>)
pub fn start(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>)
Start this
Cor
.
§Arguments
this
- The targetCor
to start.
NOTE: Beware the deadlock if it’s sync(waiting for each other), except the entry point.
Sourcepub fn set_async(&mut self, is_async: bool)
pub fn set_async(&mut self, is_async: bool)
Setup async or not.
Default async
: true
§Arguments
async
- async whentrue
, otherwisesync
.
NOTE: Beware the deadlock if it’s sync(waiting for each other), except the entry point.
Sourcepub fn is_started(&self) -> bool
pub fn is_started(&self) -> bool
Did this Cor
start?
Return true
when it did started (no matter it has stopped or not)