Struct may::coroutine::Builder

source ·
pub struct Builder { /* private fields */ }
Expand description

Coroutine factory, which can be used in order to configure the properties of a new coroutine.

Methods can be chained on it in order to configure it.

The two configurations available are:

The spawn method will take ownership of the builder and create an io::Result to the coroutine handle with the given configuration.

The coroutine::spawn free function uses a Builder with default configuration and unwraps its return value.

You may want to use spawn instead of coroutine::spawn, when you want to recover from a failure to launch a coroutine, indeed the free function will panics where the Builder method will return a io::Result.

§Examples

use may::coroutine;

let builder = coroutine::Builder::new();
let code = || {
    // coroutine code
};

let handler = unsafe { builder.spawn(code).unwrap() };

handler.join().unwrap();

Implementations§

source§

impl Builder

source

pub fn new() -> Builder

Generates the base configuration for spawning a coroutine, from which configuration methods can be chained.

source

pub fn name(self, name: String) -> Builder

Names the thread-to-be. Currently the name is used for identification only in panic messages.

source

pub fn stack_size(self, size: usize) -> Builder

Sets the size of the stack for the new coroutine.

source

pub fn id(self, id: usize) -> Builder

Sets the id of the coroutine, would select a specific thread to run

source

pub unsafe fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Spawns a new coroutine by taking ownership of the Builder, and returns an io::Result to its JoinHandle.

The spawned coroutine may outlive the caller. The join handle can be used to block on termination of the child thread, including recovering its panics.

§Errors

Unlike the spawn free function, this method yields an io::Result to capture any failure to create the thread at the OS level.

§Safety
  • Access TLS in coroutine may trigger undefined behavior.
  • If the coroutine exceed the stack during execution, this would trigger memory segment fault

If you find it annoying to wrap every thing in the unsafe block, you can use the go! macro instead.

§Examples
use may::coroutine;

let builder = coroutine::Builder::new();

let handler = unsafe {
    builder.spawn(|| {
        // thread code
    }).unwrap()
};

handler.join().unwrap();
source

pub unsafe fn spawn_local<F, T>(self, f: F) -> Result<JoinHandle<T>>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

first run the coroutine in current thread, you should always use spawn instead of this API.

§Safety

Cancel would drop all the resource of the coroutine. Normally this is safe but for some cases you should take care of the side effect

Trait Implementations§

source§

impl Default for Builder

source§

fn default() -> Builder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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

source§

const ALIGN: usize = _

The alignment of pointer.
§

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>,

§

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>,

§

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.