Struct ste::Tag[][src]

#[repr(transparent)]
pub struct Tag(_);

A tag associated with a thread. Threads which are executed with Thread support tagging.

You must ensure that any thread trying to use values first is checked with the current tag through Tag::ensure_on_thread. This includes everything which poses a potential thread safety risk.

This includes, but is not limited to:

  • Accessing or mutating any racy data or APIs, such as Cell<T>.
  • The type that is tagged, (and any nested types) drop implementation.

If all of the above is satifised, you can safely implement Send and Sync for the type. Make sure to include a comprehensive safety message such as:

// Safety: the structure is explicitly tagged with the thread that created
// it, and we ensure everywhere (including drop implementations) where racy
// access might occur that it is on the thread that created it.
unsafe impl Send for Foo {}

Tags can only be correctly constructed in two ways:

Examples

use std::cell::Cell;

struct Foo {
    tag: ste::Tag,
    data: Cell<usize>,
}

impl Foo {
    fn new() -> Self {
        Self {
            tag: ste::Tag::current_thread(),
            data: Cell::new(42),
        }
    }

    fn say_hello(&self) {
        self.tag.ensure_on_thread();
        println!("Hello from Foo: {}", self.data.get());
    }
}

// Safety: the structure is explicitly tagged with the thread that created
// it, and we ensure everywhere (including drop implementations) where racy
// access might occur that it is on the thread that created it.
unsafe impl Send for Foo {}
unsafe impl Sync for Foo {}

let thread = ste::spawn();

let foo = thread.submit(|| Foo::new());

assert!(!foo.tag.is_on_thread());

thread.submit(|| foo.say_hello());

thread.join();

Incorrect use of the tagged struct must panic:

let thread = ste::spawn();

let foo = thread.submit(|| Foo::new());

assert!(!foo.tag.is_on_thread());

foo.say_hello(); // <- oops, this panics!

thread.join();

Implementations

impl Tag[src]

pub fn current_thread() -> Self[src]

Get the tag associated with the current thread.

See Tag documentation for how to use correctly.

Panics

Panics if not running on a tagged thread. Tagged threads are the ones created with Thread.

pub fn ensure_on_thread(&self)[src]

Ensure that the tag is currently executing on the thread that created it.

See Tag documentation for how to use.

Panics

Panics if not running on a tagged thread. Tagged threads are the ones created with Thread.

Also panics unless called on the same thread that the tag was created on.

pub fn is_on_thread(&self) -> bool[src]

Test if we’re currently on the tagged thread.

See Tag documentation for how to use.

Trait Implementations

impl Clone for Tag[src]

impl Copy for Tag[src]

impl Debug for Tag[src]

impl Eq for Tag[src]

impl Hash for Tag[src]

impl PartialEq<Tag> for Tag[src]

impl StructuralEq for Tag[src]

impl StructuralPartialEq for Tag[src]

Auto Trait Implementations

impl RefUnwindSafe for Tag

impl Send for Tag

impl Sync for Tag

impl Unpin for Tag

impl UnwindSafe for Tag

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.