1
1
use crate :: client:: Client ;
2
- use crate :: cmd:: { Command , Get , Set } ;
3
2
use crate :: Result ;
4
3
5
4
use bytes:: Bytes ;
@@ -34,6 +33,13 @@ pub fn buffer(client: Client) -> Buffer {
34
33
Buffer { tx }
35
34
}
36
35
36
+ // Enum used to message pass the requested command from the `Buffer` handle
37
+ #[ derive( Debug ) ]
38
+ enum Command {
39
+ Get ( String ) ,
40
+ Set ( String , Bytes ) ,
41
+ }
42
+
37
43
// Message type sent over the channel to the connection task.
38
44
//
39
45
// `Command` is the command to forward to the connection.
@@ -52,17 +58,8 @@ async fn run(mut client: Client, mut rx: Receiver<Message>) {
52
58
while let Some ( ( cmd, tx) ) = rx. recv ( ) . await {
53
59
// The command is forwarded to the connection
54
60
let response = match cmd {
55
- Command :: Get ( get) => {
56
- let key = get. key ( ) ;
57
- client. get ( & key) . await
58
- }
59
- Command :: Set ( set) => {
60
- let key = set. key ( ) ;
61
- let value = set. value ( ) . clone ( ) ;
62
-
63
- client. set ( & key, value) . await . map ( |_| None )
64
- }
65
- _ => unreachable ! ( ) ,
61
+ Command :: Get ( key) => client. get ( & key) . await ,
62
+ Command :: Set ( key, value) => client. set ( & key, value) . await . map ( |_| None ) ,
66
63
} ;
67
64
68
65
// Send the response back to the caller.
@@ -85,13 +82,13 @@ impl Buffer {
85
82
/// connection has the ability to send the request.
86
83
pub async fn get ( & mut self , key : & str ) -> Result < Option < Bytes > > {
87
84
// Initialize a new `Get` command to send via the channel.
88
- let get = Get :: new ( key) ;
85
+ let get = Command :: Get ( key. into ( ) ) ;
89
86
90
87
// Initialize a new oneshot to be used to receive the response back from the connection.
91
88
let ( tx, rx) = oneshot:: channel ( ) ;
92
89
93
90
// Send the request
94
- self . tx . send ( ( Command :: Get ( get) , tx) ) . await ?;
91
+ self . tx . send ( ( get, tx) ) . await ?;
95
92
96
93
// Await the response
97
94
match rx. await {
@@ -106,13 +103,13 @@ impl Buffer {
106
103
/// connection has the ability to send the request
107
104
pub async fn set ( & mut self , key : & str , value : Bytes ) -> Result < ( ) > {
108
105
// Initialize a new `Set` command to send via the channel.
109
- let get = Set :: new ( key, value, None ) ;
106
+ let set = Command :: Set ( key. into ( ) , value) ;
110
107
111
108
// Initialize a new oneshot to be used to receive the response back from the connection.
112
109
let ( tx, rx) = oneshot:: channel ( ) ;
113
110
114
111
// Send the request
115
- self . tx . send ( ( Command :: Set ( get ) , tx) ) . await ?;
112
+ self . tx . send ( ( set , tx) ) . await ?;
116
113
117
114
// Await the response
118
115
match rx. await {
0 commit comments