Struct wnf::BorrowedState
source · pub struct BorrowedState<'a, T>where
T: ?Sized,{ /* private fields */ }
Expand description
A borrowed state
This has a lifetime parameter to tie it to something that owns the state, typically an OwnedState<T>
.
Unlike OwnedState<T>
, this implements Copy
(and Clone
) and does not delete the represented state on
drop.
While borrowing in Rust usually refers to borrowing memory, this applies the idea of borrowing to an external
entity, namely a state. It’s similar to BorrowedHandle<'_>
in that
regard.
Calling Clone::clone
on a BorrowedState<'a, T>
just makes a trivial copy, returning another
BorrowedState<'a, T>
with the same lifetime as the original one and representing the same
underlying WNF state. The same applies to the ToOwned::to_owned
method. If you want to turn a
BorrowedState<'_, T>
into an OwnedState<T>
(which will then delete the underlying state on
drop), use the BorrowedState::to_owned_state
method.
The type parameter T
is the type of data contained in the state.
Implementations§
source§impl<T> BorrowedState<'_, T>where
T: Read<T> + NoUninit,
impl<T> BorrowedState<'_, T>where
T: Read<T> + NoUninit,
source§impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + NoUninit + ?Sized,
impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + NoUninit + ?Sized,
source§impl<T> BorrowedState<'_, T>where
T: ?Sized,
impl<T> BorrowedState<'_, T>where
T: ?Sized,
sourcepub fn subscribers_present(self) -> Result<bool>
pub fn subscribers_present(self) -> Result<bool>
Returns whether this state has at least one subscriber
sourcepub fn is_quiescent(self) -> Result<bool>
pub fn is_quiescent(self) -> Result<bool>
Returns whether this state is “quiescent”, i.e. none of the listeners subscribed to it are currently running
source§impl<T> BorrowedState<'static, T>where
T: ?Sized,
impl<T> BorrowedState<'static, T>where
T: ?Sized,
sourcepub fn create_temporary() -> Result<Self>
pub fn create_temporary() -> Result<Self>
Creates a BorrowedState<'static, T>
with temporary lifetime and machine
scope
This is a convenience method for quickly creating a state, e.g. for testing purposes.
For more precise control over the created state, use the StateCreation
builder.
Note that a newly created state is initialized with data of size zero. This means that unless the data type T
is zero-sized or a slice type, you need to update the state data with a value of type T
before querying it
for the first time.
Errors
Returns an error if creating the state fails
source§impl<T> BorrowedState<'_, T>where
T: ?Sized,
impl<T> BorrowedState<'_, T>where
T: ?Sized,
source§impl<T> BorrowedState<'_, T>where
T: Read<T>,
impl<T> BorrowedState<'_, T>where
T: Read<T>,
sourcepub fn get(self) -> Result<T>
pub fn get(self) -> Result<T>
Queries the data of this state
See OwnedState::get
sourcepub fn query(self) -> Result<StampedData<T>>
pub fn query(self) -> Result<StampedData<T>>
Queries the data of this state together with its change stamp
source§impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + ?Sized,
impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + ?Sized,
sourcepub fn query_boxed(self) -> Result<StampedData<Box<T>>>
pub fn query_boxed(self) -> Result<StampedData<Box<T>>>
Queries the data of this state as a box together with its change stamp
source§impl<T> BorrowedState<'_, T>where
T: ?Sized,
impl<T> BorrowedState<'_, T>where
T: ?Sized,
sourcepub fn change_stamp(self) -> Result<ChangeStamp>
pub fn change_stamp(self) -> Result<ChangeStamp>
Queries the change stamp of this state
source§impl<T> BorrowedState<'_, T>where
T: Read<T> + NoUninit,
impl<T> BorrowedState<'_, T>where
T: Read<T> + NoUninit,
source§impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + NoUninit + ?Sized,
impl<T> BorrowedState<'_, T>where
T: Read<Box<T>> + NoUninit + ?Sized,
sourcepub fn replace_boxed(self, new_value: &T) -> Result<Box<T>>
pub fn replace_boxed(self, new_value: &T) -> Result<Box<T>>
Replaces the data of this state, returning the previous value as a box
source§impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
sourcepub const fn state_name(self) -> StateName
pub const fn state_name(self) -> StateName
Returns the name of this state
sourcepub const fn to_owned_state(self) -> OwnedState<T>
pub const fn to_owned_state(self) -> OwnedState<T>
Turns this BorrowedState<'_, T>
into an OwnedState<T>
representing the same underlying
state
Note that the underlying state will be deleted when the OwnedState<T>
is dropped.
sourcepub const fn cast<U>(self) -> BorrowedState<'a, U>where
U: ?Sized,
pub const fn cast<U>(self) -> BorrowedState<'a, U>where
U: ?Sized,
Casts the data type of this state to a different type U
The returned BorrowedState<'a, U>
represents the same underlying state, but treats it as
containing data of a different type U
.
source§impl<T> BorrowedState<'static, T>where
T: ?Sized,
impl<T> BorrowedState<'static, T>where
T: ?Sized,
sourcepub fn from_state_name(state_name: impl Into<StateName>) -> Self
pub fn from_state_name(state_name: impl Into<StateName>) -> Self
Statically borrows the state with the given name
Note that an underlying state with the given name may or may not exist. The returned
BorrowedState<'static, T>
having a 'static
lifetime just means that the state is borrowed
directly from the system rather than from an OwnedState<T>
that will be dropped at some point.
sourcepub fn from_state_name_and_type_id(
state_name: impl Into<StateName>,
type_id: impl Into<GUID>
) -> Self
pub fn from_state_name_and_type_id(
state_name: impl Into<StateName>,
type_id: impl Into<GUID>
) -> Self
Statically borrows the state with the given name using the given type id
Note that an underlying state with the given name may or may not exist. The returned
BorrowedState<'static, T>
having a 'static
lifetime just means that the state is borrowed
directly from the system rather than from an OwnedState<T>
that will be dropped at some point.
source§impl<T> BorrowedState<'_, T>where
T: NoUninit + ?Sized,
impl<T> BorrowedState<'_, T>where
T: NoUninit + ?Sized,
source§impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
sourcepub fn subscribe<F>(
self,
listener: F,
last_seen_change_stamp: SeenChangeStamp
) -> Result<Subscription<'a, F>>where
F: StateListener<T> + Send + 'static,
Available on crate feature subscribe
only.
pub fn subscribe<F>(
self,
listener: F,
last_seen_change_stamp: SeenChangeStamp
) -> Result<Subscription<'a, F>>where
F: StateListener<T> + Send + 'static,
subscribe
only.Subscribes the given state listener to this state
source§impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
impl<'a, T> BorrowedState<'a, T>where
T: ?Sized,
sourcepub fn wait_async(self) -> Wait<'a> ⓘ
Available on crate feature wait_async
only.
pub fn wait_async(self) -> Wait<'a> ⓘ
wait_async
only.Waits until this state is updated
source§impl<'a, T> BorrowedState<'a, T>where
T: Read<T>,
impl<'a, T> BorrowedState<'a, T>where
T: Read<T>,
source§impl<'a, T> BorrowedState<'a, T>where
T: Read<Box<T>> + ?Sized,
impl<'a, T> BorrowedState<'a, T>where
T: Read<Box<T>> + ?Sized,
sourcepub fn wait_until_boxed_async<F>(self, predicate: F) -> WaitUntilBoxed<'a, T, F> ⓘwhere
F: FnMut(&T) -> bool,
Available on crate feature wait_async
only.
pub fn wait_until_boxed_async<F>(self, predicate: F) -> WaitUntilBoxed<'a, T, F> ⓘwhere
F: FnMut(&T) -> bool,
wait_async
only.Waits until the data of this state satisfy a given predicate, returning the data as a box
source§impl<T> BorrowedState<'_, T>where
T: ?Sized,
impl<T> BorrowedState<'_, T>where
T: ?Sized,
sourcepub fn wait_blocking(self, timeout: Duration) -> Result<()>
Available on crate feature wait_blocking
only.
pub fn wait_blocking(self, timeout: Duration) -> Result<()>
wait_blocking
only.Waits until this state is updated