Skip to content

Commit 98e712a

Browse files
authored
add a specific Command enum to buffer.rs (#57)
1 parent 1cb8ec9 commit 98e712a

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/buffer.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::client::Client;
2-
use crate::cmd::{Command, Get, Set};
32
use crate::Result;
43

54
use bytes::Bytes;
@@ -34,6 +33,13 @@ pub fn buffer(client: Client) -> Buffer {
3433
Buffer { tx }
3534
}
3635

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+
3743
// Message type sent over the channel to the connection task.
3844
//
3945
// `Command` is the command to forward to the connection.
@@ -52,17 +58,8 @@ async fn run(mut client: Client, mut rx: Receiver<Message>) {
5258
while let Some((cmd, tx)) = rx.recv().await {
5359
// The command is forwarded to the connection
5460
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),
6663
};
6764

6865
// Send the response back to the caller.
@@ -85,13 +82,13 @@ impl Buffer {
8582
/// connection has the ability to send the request.
8683
pub async fn get(&mut self, key: &str) -> Result<Option<Bytes>> {
8784
// Initialize a new `Get` command to send via the channel.
88-
let get = Get::new(key);
85+
let get = Command::Get(key.into());
8986

9087
// Initialize a new oneshot to be used to receive the response back from the connection.
9188
let (tx, rx) = oneshot::channel();
9289

9390
// Send the request
94-
self.tx.send((Command::Get(get), tx)).await?;
91+
self.tx.send((get, tx)).await?;
9592

9693
// Await the response
9794
match rx.await {
@@ -106,13 +103,13 @@ impl Buffer {
106103
/// connection has the ability to send the request
107104
pub async fn set(&mut self, key: &str, value: Bytes) -> Result<()> {
108105
// 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);
110107

111108
// Initialize a new oneshot to be used to receive the response back from the connection.
112109
let (tx, rx) = oneshot::channel();
113110

114111
// Send the request
115-
self.tx.send((Command::Set(get), tx)).await?;
112+
self.tx.send((set, tx)).await?;
116113

117114
// Await the response
118115
match rx.await {

0 commit comments

Comments
 (0)