Skip to main content

Builder

Struct Builder 

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

Fluent thread-spawn configuration. Construct via Thread::builder.

Default-constructable so chains like Builder::new().name(c"foo").spawn(...) also work. Required-field validation happens at spawn time; missing name uses an empty default which most RTOSes accept.

Implementations§

Source§

impl Builder

Source

pub const fn new() -> Self

Create a new builder with default settings: no name, normal priority, 4 KB stack.

Source

pub fn name(self, name: &'static CStr) -> Self

Set the thread name. Use a c"..." literal (Rust 1.77+) to guarantee null termination at compile time.

Source

pub fn priority(self, priority: Priority) -> Self

Set the thread priority.

Source

pub fn stack_size(self, stack_size: usize) -> Self

Set the stack size in bytes.

Source

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

Spawn a thread with a FnOnce(StopToken) closure.

Cooperative cancellation is built in: when the returned JoinHandle goes out of scope, Drop calls JoinHandle::request_stop before waiting for the worker so a while !tok.is_stopped() loop exits cleanly. Use JoinHandle::detach to opt out of the join-on-drop.

The closure is heap-allocated; requires the alloc feature and a registered #[global_allocator].

§Errors

Returns Error::NoMemory on allocation failure or another error if the RTOS rejects the descriptor.

Source

pub fn spawn_cooperative(self, entry: fn(StopToken)) -> Result<JoinHandle<()>>

Spawn a thread with a stateless fn(StopToken) entry.

Heap-allocator-free variant of Builder::spawn. The entry pointer is round-tripped through *mut c_void (guarded by a compile-time size check). No captures supported.

§Errors

See Builder::spawn.

Source

pub fn spawn_simple(self, entry: fn()) -> Result<JoinHandle<()>>

Spawn a thread with a stateless fn() entry (no StopToken).

Use when the worker is a self-terminating one-shot — it returns from its entry function on its own. Drop on the returned JoinHandle still requests stop (no-op if the worker doesn’t observe), then waits for the entry function to return.

§Errors

See Builder::spawn.

Trait Implementations§

Source§

impl Default for Builder

Source§

fn default() -> Self

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