dtypes/redis/
string.rs

1//! # String Type
2//! This module contains the string type.
3use crate::redis::types::Generic;
4use std::ops::{Add, AddAssign};
5
6pub type TString = Generic<String>;
7
8impl PartialEq<&str> for TString {
9    fn eq(&self, other: &&str) -> bool {
10        self.cache.as_ref().map_or(false, |v| v == *other)
11    }
12}
13
14impl Add<&TString> for TString {
15    type Output = TString;
16
17    fn add(mut self, rhs: &TString) -> Self::Output {
18        self += rhs;
19        self
20    }
21}
22
23impl AddAssign<&str> for TString {
24    fn add_assign(&mut self, rhs: &str) {
25        let value = self.cache.take();
26        let value = match value {
27            Some(mut value) => {
28                value.push_str(rhs);
29                value
30            }
31            None => rhs.to_string(),
32        };
33        self.store(value);
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_string() {
43        let client = redis::Client::open("redis://localhost/").unwrap();
44        let mut s1 = TString::with_value("Hello".to_string(), "s1", client.clone());
45        let mut s2 = TString::with_value("World".to_string(), "s2", client.clone());
46        let mut s3 = TString::with_value("Together".to_string(), "s3", client.clone());
47        assert_eq!(s1, "Hello");
48        assert_eq!(s2, "World");
49        s1 = s1 + &s2;
50        assert_eq!(s1, "HelloWorld");
51        s2 = s1 + &s3;
52        assert_eq!(s2, "HelloWorldTogether");
53        s3 += "test";
54        assert_eq!(s3, "Togethertest");
55    }
56
57    #[test]
58    fn test_partialeq() {
59        let client = redis::Client::open("redis://localhost/").unwrap();
60        let s1 = TString::with_value("Hello".to_string(), "s1", client.clone());
61        assert_eq!(s1, "Hello");
62        assert_ne!(s1, "World");
63    }
64}