pub struct Versioned<T>where
T: Data,{ /* private fields */ }
Expand description
The core structure of this package, which provides synchronous read-access to the latest version and aysnchronous writes, delegated to the update task.
The struct is not Sync, but it is Send; in order to share between tasks it should be cloned and Sent. The contained data is not cloned; clones share the same backing linked list of versions and data.
Old versions are not actively purged, but will be dropped as long as there are no more instances holding that version. This means that instances that are held for a long duration without being accessed will prevent the old version from being purged. This situation should be avoided.
Implementations§
Source§impl<T> Versioned<T>where
T: Data,
impl<T> Versioned<T>where
T: Data,
Sourcepub fn from_initial(data: T) -> (Self, Quitter<T>)
pub fn from_initial(data: T) -> (Self, Quitter<T>)
Creates the Versioned from the initial data, returning both the Versioned instance and a Quitter which can be used to stop the backing update task.
Sourcepub fn with_latest<U, F: FnOnce(&T) -> U>(&self, action: F) -> U
pub fn with_latest<U, F: FnOnce(&T) -> U>(&self, action: F) -> U
Passes a reference to the latest version of the contained data to the provided function and returns it result.
This is the mechanism for read access to the data.
Sourcepub fn update(
&self,
update_fn: Box<dyn DataUpdater<T>>,
) -> Result<(), Box<dyn DataUpdater<T>>>
pub fn update( &self, update_fn: Box<dyn DataUpdater<T>>, ) -> Result<(), Box<dyn DataUpdater<T>>>
Allows the data to be upated by passing the latest version to the provided DataUpdater, and storing the result if one is provided. The update is delegated to the update task, which is also where the DataUpdater will be called.