Local

Struct Local 

Source
pub struct Local<T> { /* private fields */ }
Expand description

Local is a custom smart pointer-like structure designed to manage T in a single-threaded, non-concurrent context.

This design is aligned with the shared-nothing concurrency model, which avoids shared memory between threads to reduce the complexity of synchronization.

§What does concurrent context mean in single-threaded execution?

§Invalid code (with single-threaded concurrency):

use orengine::Local;


async fn write_and_dump_data_and_return_was_sent(counter: Local<Data>) -> bool {
    let mut local_ref = counter.borrow_mut();
    if is_valid_data(&*local_ref) {
        dump_data(&*local_ref).await; // Here the executor can execute other task, that change the data to invalid
        send_data(&*local_ref).await;

        return true;
    }

    false
}

§Safety

  • Local is used in one thread;
  • Before every await operation, LocalRef and LocalRefMut need to be dropped, if other task can mutate the value and.

It is checked in debug mode. If you cannot guarantee compliance with the above rules you should use LocalMutex or LocalRWLock instead. Local synchronization-primitives have almost the same performance as Local or Rc<RefCell> and can be used in single-threaded concurrency context.

§Shared-Nothing and spawn_local

In Rust, when working with local, task-specific data, you may want to leverage shared-nothing concurrency by isolating data to specific threads. Using the Local struct with spawn_local (which ensures that a task runs in a local thread) allows you to maintain this isolation. Since Local cannot be transferred to another thread, it enforces the shared-nothing principle, where each every thread holds and manages its own data independently.

§Example

use orengine::local::Local;
use orengine::Executor;
use orengine::local_executor;

Executor::init().run_with_local_future(async {
    let local_data = Local::new(42);

    println!("The value is: {}", local_data);

    *local_data.borrow_mut() += 1;

    println!("Updated value: {}", local_data);

    // Cloning (increments the internal counter)
    let cloned_data = local_data.clone();
    local_executor().spawn_local(async move {
        assert_eq!(*cloned_data.borrow(), 43);
    });
});

Implementations§

Source§

impl<T> Local<T>

Source

pub fn new(data: T) -> Self

Creates a new Local instance, initializing it with the provided data.

§Panics

If the Executor is not initialized.

Read MSG_LOCAL_EXECUTOR_IS_NOT_INIT for more details.

Source

pub fn borrow(&self) -> LocalRef<'_, T>

Returns LocalRef that allows shared access to the data.

Read more in LocalRef.

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

Source

pub fn borrow_mut(&self) -> LocalRefMut<'_, T>

Returns LocalRefMut that allows mutable access to the data.

Read more in LocalRefMut.

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

Trait Implementations§

Source§

impl<T> Clone for Local<T>

Source§

fn clone(&self) -> Self

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
Source§

impl<T: Debug> Debug for Local<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

impl<T: Default> Default for Local<T>

Source§

fn default() -> Self

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

impl<T: Display> Display for Local<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

impl<T> Drop for Local<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<T> for Local<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord> Ord for Local<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Local<T>

Source§

fn eq(&self, other: &Self) -> bool

§Panics

Panics in debug mode if the value in Local is currently mutably borrowed or if Local has been moved to another thread.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Local<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

fn lt(&self, other: &Self) -> bool

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

fn le(&self, other: &Self) -> bool

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

fn gt(&self, other: &Self) -> bool

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

fn ge(&self, other: &Self) -> bool

§Panics

Panics in debug mode if the value in either Local is currently mutably borrowed or if Local has been moved to another thread.

Source§

impl<T: Eq> Eq for Local<T>

Source§

impl<T> Sync for Local<T>

Auto Trait Implementations§

§

impl<T> Freeze for Local<T>

§

impl<T> RefUnwindSafe for Local<T>
where T: RefUnwindSafe,

§

impl<T> !Send for Local<T>

§

impl<T> Unpin for Local<T>

§

impl<T> UnwindSafe for Local<T>
where T: RefUnwindSafe,

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

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.
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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.