Connection

Struct Connection 

Source
pub struct Connection<T: Read + Write> { /* private fields */ }

Implementations§

Source§

impl<T: Read + Write> Connection<T>

Source

pub fn new(connection: T) -> Self

Examples found in repository?
examples/interact.rs (line 12)
6fn main() -> Result<()> {
7    let mut addr = String::new();
8    print!("Please enter address: ");
9    io::stdout().flush()?;
10    io::stdin().read_line(&mut addr)?;
11    let stream = TcpStream::connect(addr.trim_end())?;
12    let mut client = Connection::new(stream);
13    loop {
14        let mut buf = String::new();
15        io::stdin().read_line(&mut buf)?;
16        let action: Vec<&str> = buf.trim_end().split(' ').collect();
17        debug_assert!(action.len() > 0);
18        match action[0].to_lowercase().as_str() {
19            "set" => {
20                debug_assert!(action.len() == 3);
21                client.set(
22                    action[1].as_bytes().to_vec().into(),
23                    action[2].as_bytes().to_vec(),
24                )?;
25                println!("OK")
26            }
27
28            "get" => {
29                debug_assert!(action.len() == 2);
30                let value = client.get(action[1].as_bytes().to_vec().into())?;
31                match value {
32                    Some(data) => println!("{}", String::from_utf8(data).unwrap()),
33                    None => println!("<None>"),
34                }
35            }
36
37            "delete" => {
38                debug_assert!(action.len() == 2);
39                client.delete(action[1].as_bytes().to_vec().into())?;
40                println!("OK")
41            }
42
43            "scan" => {
44                let mut lower_key = String::new();
45                let mut upper_key = String::new();
46                print!("lower_bound(default <None>): ");
47                io::stdout().flush()?;
48                io::stdin().read_line(&mut lower_key)?;
49                let lower_bound = match lower_key.trim_end() {
50                    "" => None,
51                    key => Some(key.as_bytes().to_vec().into()),
52                };
53
54                print!("upper_bound(default <None>): ");
55                io::stdout().flush()?;
56                io::stdin().read_line(&mut upper_key)?;
57                let upper_bound = match upper_key.trim_end() {
58                    "" => None,
59                    key => Some(key.as_bytes().to_vec().into()),
60                };
61                let scanner = client.scan(lower_bound, upper_bound)?;
62                let mut counter = 0;
63                for item in scanner {
64                    let (key, value) = item?;
65                    println!(
66                        "{}: {}",
67                        String::from_utf8(key.to_vec()).unwrap(),
68                        String::from_utf8(value).unwrap()
69                    );
70                    counter += 1;
71                }
72                println!("{} items:", counter);
73            }
74            _ => {
75                break Err(Error::new(
76                    StatusCode::UnknownAction,
77                    format!("unknown action: {}", action[0]),
78                ));
79            }
80        }
81    }
82}
Source

pub fn set(&mut self, key: Key, value: Value) -> Result<()>

Examples found in repository?
examples/interact.rs (lines 21-24)
6fn main() -> Result<()> {
7    let mut addr = String::new();
8    print!("Please enter address: ");
9    io::stdout().flush()?;
10    io::stdin().read_line(&mut addr)?;
11    let stream = TcpStream::connect(addr.trim_end())?;
12    let mut client = Connection::new(stream);
13    loop {
14        let mut buf = String::new();
15        io::stdin().read_line(&mut buf)?;
16        let action: Vec<&str> = buf.trim_end().split(' ').collect();
17        debug_assert!(action.len() > 0);
18        match action[0].to_lowercase().as_str() {
19            "set" => {
20                debug_assert!(action.len() == 3);
21                client.set(
22                    action[1].as_bytes().to_vec().into(),
23                    action[2].as_bytes().to_vec(),
24                )?;
25                println!("OK")
26            }
27
28            "get" => {
29                debug_assert!(action.len() == 2);
30                let value = client.get(action[1].as_bytes().to_vec().into())?;
31                match value {
32                    Some(data) => println!("{}", String::from_utf8(data).unwrap()),
33                    None => println!("<None>"),
34                }
35            }
36
37            "delete" => {
38                debug_assert!(action.len() == 2);
39                client.delete(action[1].as_bytes().to_vec().into())?;
40                println!("OK")
41            }
42
43            "scan" => {
44                let mut lower_key = String::new();
45                let mut upper_key = String::new();
46                print!("lower_bound(default <None>): ");
47                io::stdout().flush()?;
48                io::stdin().read_line(&mut lower_key)?;
49                let lower_bound = match lower_key.trim_end() {
50                    "" => None,
51                    key => Some(key.as_bytes().to_vec().into()),
52                };
53
54                print!("upper_bound(default <None>): ");
55                io::stdout().flush()?;
56                io::stdin().read_line(&mut upper_key)?;
57                let upper_bound = match upper_key.trim_end() {
58                    "" => None,
59                    key => Some(key.as_bytes().to_vec().into()),
60                };
61                let scanner = client.scan(lower_bound, upper_bound)?;
62                let mut counter = 0;
63                for item in scanner {
64                    let (key, value) = item?;
65                    println!(
66                        "{}: {}",
67                        String::from_utf8(key.to_vec()).unwrap(),
68                        String::from_utf8(value).unwrap()
69                    );
70                    counter += 1;
71                }
72                println!("{} items:", counter);
73            }
74            _ => {
75                break Err(Error::new(
76                    StatusCode::UnknownAction,
77                    format!("unknown action: {}", action[0]),
78                ));
79            }
80        }
81    }
82}
Source

pub fn delete(&mut self, key: Key) -> Result<()>

Examples found in repository?
examples/interact.rs (line 39)
6fn main() -> Result<()> {
7    let mut addr = String::new();
8    print!("Please enter address: ");
9    io::stdout().flush()?;
10    io::stdin().read_line(&mut addr)?;
11    let stream = TcpStream::connect(addr.trim_end())?;
12    let mut client = Connection::new(stream);
13    loop {
14        let mut buf = String::new();
15        io::stdin().read_line(&mut buf)?;
16        let action: Vec<&str> = buf.trim_end().split(' ').collect();
17        debug_assert!(action.len() > 0);
18        match action[0].to_lowercase().as_str() {
19            "set" => {
20                debug_assert!(action.len() == 3);
21                client.set(
22                    action[1].as_bytes().to_vec().into(),
23                    action[2].as_bytes().to_vec(),
24                )?;
25                println!("OK")
26            }
27
28            "get" => {
29                debug_assert!(action.len() == 2);
30                let value = client.get(action[1].as_bytes().to_vec().into())?;
31                match value {
32                    Some(data) => println!("{}", String::from_utf8(data).unwrap()),
33                    None => println!("<None>"),
34                }
35            }
36
37            "delete" => {
38                debug_assert!(action.len() == 2);
39                client.delete(action[1].as_bytes().to_vec().into())?;
40                println!("OK")
41            }
42
43            "scan" => {
44                let mut lower_key = String::new();
45                let mut upper_key = String::new();
46                print!("lower_bound(default <None>): ");
47                io::stdout().flush()?;
48                io::stdin().read_line(&mut lower_key)?;
49                let lower_bound = match lower_key.trim_end() {
50                    "" => None,
51                    key => Some(key.as_bytes().to_vec().into()),
52                };
53
54                print!("upper_bound(default <None>): ");
55                io::stdout().flush()?;
56                io::stdin().read_line(&mut upper_key)?;
57                let upper_bound = match upper_key.trim_end() {
58                    "" => None,
59                    key => Some(key.as_bytes().to_vec().into()),
60                };
61                let scanner = client.scan(lower_bound, upper_bound)?;
62                let mut counter = 0;
63                for item in scanner {
64                    let (key, value) = item?;
65                    println!(
66                        "{}: {}",
67                        String::from_utf8(key.to_vec()).unwrap(),
68                        String::from_utf8(value).unwrap()
69                    );
70                    counter += 1;
71                }
72                println!("{} items:", counter);
73            }
74            _ => {
75                break Err(Error::new(
76                    StatusCode::UnknownAction,
77                    format!("unknown action: {}", action[0]),
78                ));
79            }
80        }
81    }
82}
Source

pub fn get(&mut self, key: Key) -> Result<Option<Value>>

Examples found in repository?
examples/interact.rs (line 30)
6fn main() -> Result<()> {
7    let mut addr = String::new();
8    print!("Please enter address: ");
9    io::stdout().flush()?;
10    io::stdin().read_line(&mut addr)?;
11    let stream = TcpStream::connect(addr.trim_end())?;
12    let mut client = Connection::new(stream);
13    loop {
14        let mut buf = String::new();
15        io::stdin().read_line(&mut buf)?;
16        let action: Vec<&str> = buf.trim_end().split(' ').collect();
17        debug_assert!(action.len() > 0);
18        match action[0].to_lowercase().as_str() {
19            "set" => {
20                debug_assert!(action.len() == 3);
21                client.set(
22                    action[1].as_bytes().to_vec().into(),
23                    action[2].as_bytes().to_vec(),
24                )?;
25                println!("OK")
26            }
27
28            "get" => {
29                debug_assert!(action.len() == 2);
30                let value = client.get(action[1].as_bytes().to_vec().into())?;
31                match value {
32                    Some(data) => println!("{}", String::from_utf8(data).unwrap()),
33                    None => println!("<None>"),
34                }
35            }
36
37            "delete" => {
38                debug_assert!(action.len() == 2);
39                client.delete(action[1].as_bytes().to_vec().into())?;
40                println!("OK")
41            }
42
43            "scan" => {
44                let mut lower_key = String::new();
45                let mut upper_key = String::new();
46                print!("lower_bound(default <None>): ");
47                io::stdout().flush()?;
48                io::stdin().read_line(&mut lower_key)?;
49                let lower_bound = match lower_key.trim_end() {
50                    "" => None,
51                    key => Some(key.as_bytes().to_vec().into()),
52                };
53
54                print!("upper_bound(default <None>): ");
55                io::stdout().flush()?;
56                io::stdin().read_line(&mut upper_key)?;
57                let upper_bound = match upper_key.trim_end() {
58                    "" => None,
59                    key => Some(key.as_bytes().to_vec().into()),
60                };
61                let scanner = client.scan(lower_bound, upper_bound)?;
62                let mut counter = 0;
63                for item in scanner {
64                    let (key, value) = item?;
65                    println!(
66                        "{}: {}",
67                        String::from_utf8(key.to_vec()).unwrap(),
68                        String::from_utf8(value).unwrap()
69                    );
70                    counter += 1;
71                }
72                println!("{} items:", counter);
73            }
74            _ => {
75                break Err(Error::new(
76                    StatusCode::UnknownAction,
77                    format!("unknown action: {}", action[0]),
78                ));
79            }
80        }
81    }
82}
Source

pub fn scan( &mut self, lower_bound: Option<Key>, upper_bound: Option<Key>, ) -> Result<Box<dyn Iterator<Item = Result<Entry>> + '_>>

Examples found in repository?
examples/interact.rs (line 61)
6fn main() -> Result<()> {
7    let mut addr = String::new();
8    print!("Please enter address: ");
9    io::stdout().flush()?;
10    io::stdin().read_line(&mut addr)?;
11    let stream = TcpStream::connect(addr.trim_end())?;
12    let mut client = Connection::new(stream);
13    loop {
14        let mut buf = String::new();
15        io::stdin().read_line(&mut buf)?;
16        let action: Vec<&str> = buf.trim_end().split(' ').collect();
17        debug_assert!(action.len() > 0);
18        match action[0].to_lowercase().as_str() {
19            "set" => {
20                debug_assert!(action.len() == 3);
21                client.set(
22                    action[1].as_bytes().to_vec().into(),
23                    action[2].as_bytes().to_vec(),
24                )?;
25                println!("OK")
26            }
27
28            "get" => {
29                debug_assert!(action.len() == 2);
30                let value = client.get(action[1].as_bytes().to_vec().into())?;
31                match value {
32                    Some(data) => println!("{}", String::from_utf8(data).unwrap()),
33                    None => println!("<None>"),
34                }
35            }
36
37            "delete" => {
38                debug_assert!(action.len() == 2);
39                client.delete(action[1].as_bytes().to_vec().into())?;
40                println!("OK")
41            }
42
43            "scan" => {
44                let mut lower_key = String::new();
45                let mut upper_key = String::new();
46                print!("lower_bound(default <None>): ");
47                io::stdout().flush()?;
48                io::stdin().read_line(&mut lower_key)?;
49                let lower_bound = match lower_key.trim_end() {
50                    "" => None,
51                    key => Some(key.as_bytes().to_vec().into()),
52                };
53
54                print!("upper_bound(default <None>): ");
55                io::stdout().flush()?;
56                io::stdin().read_line(&mut upper_key)?;
57                let upper_bound = match upper_key.trim_end() {
58                    "" => None,
59                    key => Some(key.as_bytes().to_vec().into()),
60                };
61                let scanner = client.scan(lower_bound, upper_bound)?;
62                let mut counter = 0;
63                for item in scanner {
64                    let (key, value) = item?;
65                    println!(
66                        "{}: {}",
67                        String::from_utf8(key.to_vec()).unwrap(),
68                        String::from_utf8(value).unwrap()
69                    );
70                    counter += 1;
71                }
72                println!("{} items:", counter);
73            }
74            _ => {
75                break Err(Error::new(
76                    StatusCode::UnknownAction,
77                    format!("unknown action: {}", action[0]),
78                ));
79            }
80        }
81    }
82}
Source

pub fn ping(&mut self) -> Result<()>

Source

pub fn no_response(&mut self) -> Result<()>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Connection<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<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.