pub fn del(&self, key: &str) -> bool self.inner.lock().unwrap().remove(key).is_some()
Add tokio (or std::thread + std::net ) to Cargo.toml :
pub fn get(&self, key: &str) -> Option<Vec<u8>> let map = self.inner.lock().unwrap(); map.get(key).cloned()
#[derive(Debug, PartialEq)] pub enum RespValue SimpleString(String), Error(String), Integer(i64), BulkString(Option<Vec<u8>>), Array(Vec<RespValue>),
#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let addr = "127.0.0.1:6379"; let listener = TcpListener::bind(addr).await?; let store = Store::new();
We use an . Enums in Rust are algebraic data types capable of holding data. This is where Rust shines compared to C. In C, you might use a void* pointer and manual type tagging, which is error-prone. In Rust, the compiler ensures you handle every case.
Giordani recommends starting with std::thread to understand blocking I/O, then moving to tokio for async.
Some(value.data.clone()) else None
To close the gap, implement pipelining and sharding.
pub fn set(&self, key: String, value: Vec<u8>) let mut map = self.inner.lock().unwrap(); map.insert(key, value);
fn handle_dbsize(store: &Store, _args: &[RespValue]) -> RespValue RespValue::Integer(store.dbsize() as i64)
© Copyright 2019 lovelycraft.com | Free Amigurumi Patterns