-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from upupnoah/main
feat(resp-decode): support resp decode for simple redis
- Loading branch information
Showing
7 changed files
with
625 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ authors = ["Noah <[email protected]>"] | |
anyhow = "^1.0" | ||
bytes = "^1.6.1" | ||
enum_dispatch = "^0.3.13" | ||
thiserror = "^1.0.62" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use anyhow::Result; | ||
use bytes::BytesMut; | ||
fn main() -> Result<()> { | ||
let a = "hello"; // a.as_bytes() = b"hello" | ||
|
||
// bytes_mut | ||
let mut bytes_mut = BytesMut::new(); | ||
bytes_mut.extend_from_slice(a.as_bytes()); | ||
println!("bytes_mut: {:?}", bytes_mut); | ||
|
||
let b = bytes_mut.split_to(3); | ||
println!("b: {:?}", b); | ||
println!("after split_to(3) -> bytes_mut: {:?}", bytes_mut); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use enum_dispatch::enum_dispatch; | ||
|
||
#[enum_dispatch] | ||
trait DoSomething { | ||
fn do_something(&self); | ||
} | ||
|
||
#[enum_dispatch(DoSomething)] | ||
enum Types { | ||
Apple(A), | ||
Banana(B), | ||
} | ||
|
||
struct A; | ||
struct B; | ||
|
||
impl DoSomething for A { | ||
fn do_something(&self) { | ||
println!("A"); | ||
} | ||
} | ||
|
||
impl DoSomething for B { | ||
fn do_something(&self) { | ||
println!("B"); | ||
} | ||
} | ||
fn main() { | ||
// test enum_dispatch | ||
let apple = Types::Apple(A); | ||
let banana = Types::Banana(B); | ||
|
||
let type_apple = apple; | ||
let type_banana = banana; | ||
|
||
// 都是 types 类型的, 但是结果不同, enum_dispatch 相当于是主动帮我 match 了 | ||
type_apple.do_something(); | ||
type_banana.do_something(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use anyhow::{Context, Result}; | ||
use std::io::Error as IoError; | ||
use std::num::ParseIntError; | ||
use thiserror::Error; | ||
|
||
// 定义自定义错误类型 | ||
#[derive(Error, Debug)] | ||
enum MyError { | ||
#[error("An IO error occurred: {0}")] | ||
Io(#[from] IoError), | ||
|
||
#[error("A parsing error occurred: {0}")] | ||
Parse(#[from] ParseIntError), | ||
|
||
#[error("Custom error: {0}")] | ||
Custom(String), | ||
|
||
#[error("Anyhow error: {0}")] | ||
Anyhow(#[from] anyhow::Error), | ||
} | ||
|
||
// 一个可能返回错误的函数 | ||
fn parse_number(input: &str) -> Result<i32, MyError> { | ||
let trimmed = input.trim(); | ||
if trimmed.is_empty() { | ||
return Err(MyError::Custom("Input is empty".into())); | ||
} | ||
|
||
let number: i32 = trimmed | ||
.parse() | ||
// .map_err(|e| MyError::Parse(e)) | ||
.map_err(MyError::Parse) // 更好的写法 | ||
.context("Failed to parse number")?; | ||
Ok(number) | ||
} | ||
|
||
fn main() -> Result<(), MyError> { | ||
// 示例一: 正确的输入 | ||
match parse_number("42") { | ||
Ok(number) => println!("Parsed number: {}", number), | ||
Err(e) => eprintln!("Error: {}", e), | ||
} | ||
|
||
// 示例二: 空输入 | ||
match parse_number("") { | ||
Ok(number) => println!("Parsed number: {}", number), | ||
Err(e) => eprintln!("Error: {}", e), | ||
} | ||
|
||
// 示例三: 无效输入 | ||
match parse_number("abc") { | ||
Ok(number) => println!("Parsed number: {}", number), | ||
Err(e) => eprintln!("Error: {}", e), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.