pub struct Sticky<T: 'static> { /* private fields */ }Expand description
A Sticky<T> keeps a value T stored in a thread.
This type works similar in nature to Fragile and exposes a
similar interface. The difference is that whereas Fragile has
its destructor called in the thread where the value was sent, a
Sticky that is moved to another thread will have the internal
destructor called when the originating thread tears down.
Because Sticky allows values to be kept alive for longer than the
Sticky itself, it requires all its contents to be 'static for
soundness. Access to the value is limited to scoped callbacks so references
cannot outlive the callback or the originating thread.
As this uses TLS internally the general rules about the platform limitations of destructors for TLS apply.
A Sticky<T> is Unpin only if T is Unpin. This ensures that once a
wrapped value has been pinned through the Future or Stream
implementation, it cannot later be moved out of the wrapper.
Implementations§
Source§impl<T> Sticky<T>
impl<T> Sticky<T>
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Returns true if the access is valid.
This will be false if the value was sent to another thread. It is
also false once its originating registry starts being destroyed,
even on platforms that subsequently reinitialize thread local storage.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Sticky, returning the wrapped value.
§Panics
Panics if called from a different thread than the one where the original value was created, or if that thread is already destroying its thread local storage.
Sourcepub fn try_into_inner(self) -> Result<T, Self>
pub fn try_into_inner(self) -> Result<T, Self>
Consumes the Sticky, returning the wrapped value if successful.
The wrapped value is returned if this is called from the same thread
as the one where the original value was created, otherwise the
Sticky is returned as Err(self).
Sourcepub fn with<R, F>(&self, f: F) -> R
pub fn with<R, F>(&self, f: F) -> R
Invokes a callback with a shared reference to the wrapped value.
The callback may return owned data, but it cannot return a reference derived from the wrapped value. This keeps every borrow within the lifetime of the originating thread.
use fragile::Sticky;
let value = Sticky::new(String::from("hello"));
let length = value.with(String::len);
assert_eq!(length, 5);A future borrowing the value cannot escape the callback. Clone or otherwise create owned data before returning a future:
use fragile::Sticky;
let value = Sticky::new(String::from("hello"));
let future = value.with(|value| {
let value = value.clone();
async move {
std::future::ready(()).await;
assert_eq!(value, "hello");
}
});§Panics
Panics if the calling thread is not the one that wrapped the value, or
if that thread is already destroying its thread local storage.
For a non-panicking variant, use try_with.
Sourcepub fn with_mut<R, F>(&mut self, f: F) -> R
pub fn with_mut<R, F>(&mut self, f: F) -> R
Invokes a callback with an exclusive reference to the wrapped value.
The callback may return owned data, but it cannot return a reference derived from the wrapped value.
use fragile::Sticky;
let mut value = Sticky::new(String::from("hello"));
value.with_mut(|value| value.push_str(" world"));
assert_eq!(value.with(Clone::clone), "hello world");§Panics
Panics if the calling thread is not the one that wrapped the value, or
if that thread is already destroying its thread local storage.
For a non-panicking variant, use try_with_mut.
Sourcepub fn try_with<R, F>(&self, f: F) -> Result<R, InvalidThreadAccess>
pub fn try_with<R, F>(&self, f: F) -> Result<R, InvalidThreadAccess>
Invokes a callback with a shared reference to the wrapped value.
As with with, the callback cannot return a reference
derived from the wrapped value.
Returns InvalidThreadAccess without invoking the callback if
is_valid is false, that is when called from a
thread other than the one that wrapped the value or while that thread
is destroying its thread local storage.
Sourcepub fn try_with_mut<R, F>(&mut self, f: F) -> Result<R, InvalidThreadAccess>
pub fn try_with_mut<R, F>(&mut self, f: F) -> Result<R, InvalidThreadAccess>
Invokes a callback with an exclusive reference to the wrapped value.
As with with_mut, the callback cannot return a
reference derived from the wrapped value.
Returns InvalidThreadAccess without invoking the callback if
is_valid is false, that is when called from a
thread other than the one that wrapped the value or while that thread
is destroying its thread local storage.