pub struct Connection<T: Read + Write> { /* private fields */ }Implementations§
Source§impl<T: Read + Write> Connection<T>
impl<T: Read + Write> Connection<T>
Sourcepub fn new(connection: T) -> Self
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}Sourcepub fn set(&mut self, key: Key, value: Value) -> Result<()>
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}Sourcepub fn delete(&mut self, key: Key) -> Result<()>
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}Sourcepub fn get(&mut self, key: Key) -> Result<Option<Value>>
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}Sourcepub fn scan(
&mut self,
lower_bound: Option<Key>,
upper_bound: Option<Key>,
) -> Result<Box<dyn Iterator<Item = Result<Entry>> + '_>>
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}pub fn ping(&mut self) -> Result<()>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more