Guard

Struct Guard 

Source
pub struct Guard<'a, T> { /* private fields */ }
Expand description

The guard struct for the Mutex. It is used to access the value and not for you to initialize it by your own.

Implementations§

Source§

impl<'a, T> Guard<'a, T>

Source

pub fn expand(&mut self)

Expands the lock time by 2000ms from the point on its called. This is useful if you need to access the value for a longer time.

But use it with caution, because it can lead to deadlocks. To avoid deadlocks, we only allow one extension per lock.

Source

pub fn store(&mut self, value: T) -> Result<(), LockError>
where T: Serialize,

Stores the value in Redis. This function blocks until the value is stored. Disables the store operation of the guarded value.

Source

pub fn acquire(&mut self) -> &T

Loads the value from Redis. This function blocks until the value is loaded. Shadows the load operation of the guarded value.

Methods from Deref<Target = Generic<T>>§

Source

pub fn store(&mut self, value: T)

The store method sets the value of the type.

Examples found in repository?
examples/2services.rs (line 15)
5fn main() {
6    thread::scope(|s| {
7        let client = redis::Client::open("redis://localhost:6379").unwrap();
8        let client2 = client.clone();
9
10        let t1 = s.spawn(move || {
11            let mut string = String::with_value("Hello".to_string(), "test", client);
12            println!("Thread1: {}", string.cached().unwrap());
13            assert_eq!(string, "Hello");
14            sleep(std::time::Duration::from_secs(1));
15            string.store("World".to_string());
16            println!("Thread1: {}", string.cached().unwrap());
17            assert_eq!(string, "World");
18        });
19
20        let t2 = s.spawn(move || {
21            sleep(std::time::Duration::from_micros(100));
22            let mut string = String::with_load("test", client2);
23            println!("Thread2: {}", string.cached().unwrap());
24            assert_eq!(string, "Hello");
25            sleep(std::time::Duration::from_secs(2));
26            string.acquire();
27            println!("Thread2: {}", string.cached().unwrap());
28            assert_eq!(string, "World");
29        });
30        t1.join().expect("Failed to join thread1");
31        t2.join().expect("Failed to join thread2");
32    });
33}
Source

pub fn acquire(&mut self) -> &T

The acquire method returns a reference to the value stored in the type. Loads it from the redis directly.

§Example
use dtypes::redis::types::Di32 as i32;

let client = redis::Client::open("redis://localhost:6379").unwrap();
let mut i32 = i32::with_value(1, "test_add", client.clone());
i32 = i32 + i32::with_value(2, "test_add2", client);
assert_eq!(i32.acquire(), &3);
Examples found in repository?
examples/2services.rs (line 26)
5fn main() {
6    thread::scope(|s| {
7        let client = redis::Client::open("redis://localhost:6379").unwrap();
8        let client2 = client.clone();
9
10        let t1 = s.spawn(move || {
11            let mut string = String::with_value("Hello".to_string(), "test", client);
12            println!("Thread1: {}", string.cached().unwrap());
13            assert_eq!(string, "Hello");
14            sleep(std::time::Duration::from_secs(1));
15            string.store("World".to_string());
16            println!("Thread1: {}", string.cached().unwrap());
17            assert_eq!(string, "World");
18        });
19
20        let t2 = s.spawn(move || {
21            sleep(std::time::Duration::from_micros(100));
22            let mut string = String::with_load("test", client2);
23            println!("Thread2: {}", string.cached().unwrap());
24            assert_eq!(string, "Hello");
25            sleep(std::time::Duration::from_secs(2));
26            string.acquire();
27            println!("Thread2: {}", string.cached().unwrap());
28            assert_eq!(string, "World");
29        });
30        t1.join().expect("Failed to join thread1");
31        t2.join().expect("Failed to join thread2");
32    });
33}
Source

pub fn cached(&self) -> Option<&T>

The get method returns a reference to the value stored in the type.

Examples found in repository?
examples/2services.rs (line 12)
5fn main() {
6    thread::scope(|s| {
7        let client = redis::Client::open("redis://localhost:6379").unwrap();
8        let client2 = client.clone();
9
10        let t1 = s.spawn(move || {
11            let mut string = String::with_value("Hello".to_string(), "test", client);
12            println!("Thread1: {}", string.cached().unwrap());
13            assert_eq!(string, "Hello");
14            sleep(std::time::Duration::from_secs(1));
15            string.store("World".to_string());
16            println!("Thread1: {}", string.cached().unwrap());
17            assert_eq!(string, "World");
18        });
19
20        let t2 = s.spawn(move || {
21            sleep(std::time::Duration::from_micros(100));
22            let mut string = String::with_load("test", client2);
23            println!("Thread2: {}", string.cached().unwrap());
24            assert_eq!(string, "Hello");
25            sleep(std::time::Duration::from_secs(2));
26            string.acquire();
27            println!("Thread2: {}", string.cached().unwrap());
28            assert_eq!(string, "World");
29        });
30        t1.join().expect("Failed to join thread1");
31        t2.join().expect("Failed to join thread2");
32    });
33}

Trait Implementations§

Source§

impl<T> Deref for Guard<'_, T>

Source§

type Target = Generic<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for Guard<'_, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> Drop for Guard<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Guard<'a, T>

§

impl<'a, T> !RefUnwindSafe for Guard<'a, T>

§

impl<'a, T> Send for Guard<'a, T>
where T: Send,

§

impl<'a, T> Sync for Guard<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Guard<'a, T>

§

impl<'a, T> !UnwindSafe for Guard<'a, 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.