Generic

Struct Generic 

Source
pub struct Generic<T> { /* private fields */ }
Expand description

The generic type is used to implement the common methods for all types.

The generic type is not meant to be used directly. Instead use one of the aliases.

Mostly you will interact with the methods Generic::store, Generic::acquire and Generic::into_inner.

Implementations§

Source§

impl<T> Generic<T>

Source

pub fn new(field_name: &str, client: Client) -> Generic<T>

The new method creates a new instance of the type. It does not load or store any value in Redis. It only creates the instance.

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

let client = redis::Client::open("redis://localhost:6379").unwrap();
let mut i32 = i32::new("test_add", client.clone());
i32.store(1);
let i32 = i32 + i32::with_value(2, "test_add2", client);
assert_eq!(i32, 3);
Source

pub fn with_value(value: T, field_name: &str, client: Client) -> Generic<T>

The with_value method creates a new instance of the type. If a value is already stored in Redis, it will be overwritten.

Examples found in repository?
examples/2services.rs (line 11)
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 with_load(field_name: &str, client: Client) -> Generic<T>

The with_value_load method creates a new instance of the type. It loads the value from Redis. If there is no value stored in Redis, it stores a None in cache.

Examples found in repository?
examples/2services.rs (line 22)
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 with_value_default( value: T, field_name: &str, client: Client, ) -> Generic<T>

The with_value_default method creates a new instance of the type. If the value is not already stored in Redis, it will be stored. If the value is already stored in Redis, it will be loaded and your given value will be ignored.

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 into_inner(self) -> T

The into_inner method returns the inner value of the type. This method consumes the type and drops everything.

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

let client = redis::Client::open("redis://localhost:6379").unwrap();
let i32 = i32::with_value(3, "test_add", client.clone());
let i32_inner = i32.into_inner();
assert_eq!(i32_inner, 3);
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 Add<&Generic<String>> for TString

Source§

type Output = Generic<String>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &TString) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> Add<T> for Generic<T>
where T: Add<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> Add for Generic<T>
where T: Add<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Generic<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> AddAssign<T> for Generic<T>

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl<T> AddAssign for Generic<T>

Source§

fn add_assign(&mut self, rhs: Generic<T>)

Performs the += operation. Read more
Source§

impl<T> BitAnd<T> for Generic<T>
where T: BitAnd<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: T) -> Self::Output

Performs the & operation. Read more
Source§

impl<T> BitAnd for Generic<T>
where T: BitAnd<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Generic<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T> BitOr<T> for Generic<T>
where T: BitOr<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: T) -> Self::Output

Performs the | operation. Read more
Source§

impl<T> BitOr for Generic<T>
where T: BitOr<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Generic<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T> BitXor<T> for Generic<T>
where T: BitXor<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: T) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T> BitXor for Generic<T>
where T: BitXor<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Generic<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Debug> Debug for Generic<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Generic<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> Div<T> for Generic<T>
where T: Div<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> Div for Generic<T>
where T: Div<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Generic<T>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> Mul<T> for Generic<T>
where T: Mul<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> Mul for Generic<T>
where T: Mul<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Generic<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: PartialEq> PartialEq<T> for Generic<T>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for Generic<T>

Source§

fn eq(&self, other: &Generic<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub<T> for Generic<T>
where T: Sub<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Sub for Generic<T>
where T: Sub<Output = T> + Display + Serialize + DeserializeOwned,

Source§

type Output = Generic<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Generic<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> SubAssign<T> for Generic<T>

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl<T> SubAssign for Generic<T>

Source§

fn sub_assign(&mut self, rhs: Generic<T>)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Generic<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Generic<T>
where T: RefUnwindSafe,

§

impl<T> Send for Generic<T>
where T: Send,

§

impl<T> Sync for Generic<T>
where T: Sync,

§

impl<T> Unpin for Generic<T>
where T: Unpin,

§

impl<T> UnwindSafe for Generic<T>
where T: UnwindSafe,

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.