Struct anycache::FromWatchedFile

source ·
pub struct FromWatchedFile<T> { /* private fields */ }
Expand description

FromWatchedFile is a struct that reads a file and watches for changes to the file. When the file changes, the struct will reload the file and update the value in the background. This struct is useful for reloading configuration files or other files that are read frequently. It is thread safe. Note: Each FromWatchedFile spawns a new thread to watch the file do not use too many of them!

Implementations§

source§

impl<T> FromWatchedFile<T>
where T: Send + Sync + 'static,

source

pub fn new<F>(file_path: &str, parser: F, interval: Duration) -> Self
where F: Fn(&[u8]) -> T + Send + Sync + 'static,

Create a new FromWatchedFile struct and spawn a new thread with given interval and converter function. Converter function converts a slice of bytes to the desired type.

The file will be check based on interval. On change detected, the parser will be used to convert the file content to desired type.

You can get the latest copy using the get method.

Upon initialization, the first copy will be constructed.

The code never fails. If the file gone missing, or the file is not readable, the value will be None.

source

pub fn get<'a>(&'a self) -> Arc<Option<T>>

Get the desired converted type from the file If the file become not readable, it will return the last good copy.

Auto Trait Implementations§

§

impl<T> Freeze for FromWatchedFile<T>

§

impl<T> RefUnwindSafe for FromWatchedFile<T>

§

impl<T> Send for FromWatchedFile<T>
where T: Sync + Send,

§

impl<T> Sync for FromWatchedFile<T>
where T: Sync + Send,

§

impl<T> Unpin for FromWatchedFile<T>

§

impl<T> UnwindSafe for FromWatchedFile<T>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.