Skip to content

Commit

Permalink
deps: remove beef
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 11, 2024
1 parent 717e9fe commit cff6145
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ publish = true
[dependencies]
anyhow = "1"
async-trait = "0.1"
beef = { version = "0.5.1", features = ["impl_serde"] }
jsonrpsee-types = { workspace = true }
thiserror = "1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ pub mod __reexports {
}
}

pub use beef::Cow;
pub use serde::{de::DeserializeOwned, Serialize};
pub use serde_json::{
to_value as to_json_value, value::to_raw_value as to_json_raw_value, value::RawValue as JsonRawValue,
Value as JsonValue,
};
pub use std::borrow::Cow;

/// Ten megabytes.
pub const TEN_MB_SIZE_BYTES: u32 = 10 * 1024 * 1024;
Expand Down
1 change: 0 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ publish = false

[dev-dependencies]
anyhow = "1"
beef = { version = "0.5.1", features = ["impl_serde"] }
fast-socks5 = { version = "0.9.1" }
futures = { version = "0.3.14", default-features = false, features = ["std"] }
futures-util = { version = "0.3.14", default-features = false, features = ["alloc"]}
Expand Down
18 changes: 7 additions & 11 deletions tests/tests/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,14 @@ mod rpc_impl {
a: &str,
b: &'_ str,
c: std::borrow::Cow<'_, str>,
d: Option<beef::Cow<'_, str>>,
d: Option<std::borrow::Cow<'_, str>>,
) -> Result<String, ErrorObjectOwned> {
Ok(format!("Called with: {}, {}, {}, {:?}", a, b, c, d))
}

#[method(name = "zero_copy_cow")]
fn zero_copy_cow(
&self,
a: std::borrow::Cow<'_, str>,
b: beef::Cow<'_, str>,
) -> Result<String, ErrorObjectOwned> {
Ok(format!("Zero copy params: {}, {}", matches!(a, std::borrow::Cow::Borrowed(_)), b.is_borrowed()))
fn zero_copy_cow(&self, a: std::borrow::Cow<'_, str>) -> Result<String, ErrorObjectOwned> {
Ok(format!("Zero copy params: {}", matches!(a, std::borrow::Cow::Borrowed(_))))
}

#[method(name = "blocking_call", blocking)]
Expand Down Expand Up @@ -285,19 +281,19 @@ async fn macro_zero_copy_cow() {
let module = RpcServerImpl.into_rpc();

let (resp, _) = module
.raw_json_request(r#"{"jsonrpc":"2.0","method":"foo_zero_copy_cow","params":["foo", "bar"],"id":0}"#, 1)
.raw_json_request(r#"{"jsonrpc":"2.0","method":"foo_zero_copy_cow","params":["foo"],"id":0}"#, 1)
.await
.unwrap();

// std::borrow::Cow<str> always deserialized to owned variant here
assert_eq!(resp, r#"{"jsonrpc":"2.0","result":"Zero copy params: false, true","id":0}"#);
assert_eq!(resp, r#"{"jsonrpc":"2.0","result":"Zero copy params: false","id":0}"#);

// serde_json will have to allocate a new string to replace `\t` with byte 0x09 (tab)
let (resp, _) = module
.raw_json_request(r#"{"jsonrpc":"2.0","method":"foo_zero_copy_cow","params":["\tfoo", "\tbar"],"id":0}"#, 1)
.raw_json_request(r#"{"jsonrpc":"2.0","method":"foo_zero_copy_cow","params":["\tfoo"],"id":0}"#, 1)
.await
.unwrap();
assert_eq!(resp, r#"{"jsonrpc":"2.0","result":"Zero copy params: false, false","id":0}"#);
assert_eq!(resp, r#"{"jsonrpc":"2.0","result":"Zero copy params: false","id":0}"#);
}

// Disabled on MacOS as GH CI timings on Mac vary wildly (~100ms) making this test fail.
Expand Down
1 change: 0 additions & 1 deletion types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ readme.workspace = true
publish = true

[dependencies]
beef = { version = "0.5.1", features = ["impl_serde"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1", default-features = false, features = ["alloc", "raw_value", "std"] }
thiserror = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions types/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
//! Types to handle JSON-RPC request parameters according to the [spec](https://www.jsonrpc.org/specification#parameter_structures).
//! Some types come with a "*Ser" variant that implements [`serde::Serialize`]; these are used in the client.
use std::borrow::Cow;
use std::fmt;

use beef::Cow;
use serde::de::{self, Deserializer, Unexpected, Visitor};
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a> Params<'a> {
///
/// This will cause an allocation if the params internally are using a borrowed JSON slice.
pub fn into_owned(self) -> Params<'static> {
Params(self.0.map(|s| Cow::owned(s.into_owned())))
Params(self.0.map(|s| Cow::Owned(s.into_owned())))
}

/// Return the length of underlying JSON string in number of bytes.
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<'a> SubscriptionId<'a> {
pub fn into_owned(self) -> SubscriptionId<'static> {
match self {
SubscriptionId::Num(num) => SubscriptionId::Num(num),
SubscriptionId::Str(s) => SubscriptionId::Str(Cow::owned(s.into_owned())),
SubscriptionId::Str(s) => SubscriptionId::Str(Cow::Owned(s.into_owned())),
}
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ impl<'a> Id<'a> {
match self {
Id::Null => Id::Null,
Id::Number(num) => Id::Number(num),
Id::Str(s) => Id::Str(Cow::owned(s.into_owned())),
Id::Str(s) => Id::Str(Cow::Owned(s.into_owned())),
}
}

Expand Down Expand Up @@ -421,7 +421,7 @@ mod test {
let deserialized: Id = serde_json::from_str(s).unwrap();
match deserialized {
Id::Str(ref cow) => {
assert!(cow.is_borrowed());
assert!(matches!(cow, Cow::Borrowed(_)));
assert_eq!(cow, "2");
}
_ => panic!("Expected Id::Str"),
Expand All @@ -433,7 +433,7 @@ mod test {

let s = r#""2x""#;
let deserialized: Id = serde_json::from_str(s).unwrap();
assert_eq!(deserialized, Id::Str(Cow::const_str("2x")));
assert_eq!(deserialized, Id::Str(Cow::Borrowed("2x")));

let s = r#"[1337]"#;
assert!(serde_json::from_str::<Id>(s).is_err());
Expand Down
23 changes: 11 additions & 12 deletions types/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
//! Types to handle JSON-RPC requests according to the [spec](https://www.jsonrpc.org/specification#request-object).
//! Some types come with a "*Ser" variant that implements [`serde::Serialize`]; these are used in the client.
use std::borrow::Cow as StdCow;
use std::borrow::Cow;

use crate::{
params::{Id, TwoPointZero},
Params,
};
use beef::Cow;
use http::Extensions;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
Expand All @@ -51,7 +50,7 @@ pub struct Request<'a> {
pub method: Cow<'a, str>,
/// Parameter values of the request.
#[serde(borrow)]
pub params: Option<StdCow<'a, RawValue>>,
pub params: Option<Cow<'a, RawValue>>,
/// The request's extensions.
#[serde(skip)]
pub extensions: Extensions,
Expand All @@ -60,7 +59,7 @@ pub struct Request<'a> {
impl<'a> Request<'a> {
/// Create a new [`Request`].
pub fn new(method: Cow<'a, str>, params: Option<&'a RawValue>, id: Id<'a>) -> Self {
Self { jsonrpc: TwoPointZero, id, method, params: params.map(StdCow::Borrowed), extensions: Extensions::new() }
Self { jsonrpc: TwoPointZero, id, method, params: params.map(Cow::Borrowed), extensions: Extensions::new() }
}

/// Get the ID of the request.
Expand Down Expand Up @@ -129,7 +128,7 @@ pub struct RequestSer<'a> {
pub method: Cow<'a, str>,
/// Parameter values of the request.
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<StdCow<'a, RawValue>>,
pub params: Option<Cow<'a, RawValue>>,
}

impl<'a> RequestSer<'a> {
Expand All @@ -139,13 +138,13 @@ impl<'a> RequestSer<'a> {
jsonrpc: TwoPointZero,
id: id.clone(),
method: method.as_ref().into(),
params: params.map(StdCow::Borrowed),
params: params.map(Cow::Borrowed),
}
}

/// Create a owned serializable JSON-RPC method call.
pub fn owned(id: Id<'a>, method: impl Into<String>, params: Option<Box<RawValue>>) -> Self {
Self { jsonrpc: TwoPointZero, id, method: method.into().into(), params: params.map(StdCow::Owned) }
Self { jsonrpc: TwoPointZero, id, method: method.into().into(), params: params.map(Cow::Owned) }
}
}

Expand All @@ -159,24 +158,24 @@ pub struct NotificationSer<'a> {
pub method: Cow<'a, str>,
/// Parameter values of the request.
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<StdCow<'a, RawValue>>,
pub params: Option<Cow<'a, RawValue>>,
}

impl<'a> NotificationSer<'a> {
/// Create a borrowed serializable JSON-RPC notification.
pub fn borrowed(method: &'a impl AsRef<str>, params: Option<&'a RawValue>) -> Self {
Self { jsonrpc: TwoPointZero, method: method.as_ref().into(), params: params.map(StdCow::Borrowed) }
Self { jsonrpc: TwoPointZero, method: method.as_ref().into(), params: params.map(Cow::Borrowed) }
}

/// Create an owned serializable JSON-RPC notification.
pub fn owned(method: impl Into<String>, params: Option<Box<RawValue>>) -> Self {
Self { jsonrpc: TwoPointZero, method: method.into().into(), params: params.map(StdCow::Owned) }
Self { jsonrpc: TwoPointZero, method: method.into().into(), params: params.map(Cow::Owned) }
}
}

#[cfg(test)]
mod test {
use super::{Id, InvalidRequest, Notification, NotificationSer, Request, RequestSer, StdCow, TwoPointZero};
use super::{Cow, Id, InvalidRequest, Notification, NotificationSer, Request, RequestSer, TwoPointZero};
use serde_json::value::RawValue;

fn assert_request<'a>(request: Request<'a>, id: Id<'a>, method: &str, params: Option<&str>) {
Expand Down Expand Up @@ -278,7 +277,7 @@ mod test {
jsonrpc: TwoPointZero,
method: method.into(),
id: id.unwrap_or(Id::Null),
params: params.map(StdCow::Owned),
params: params.map(Cow::Owned),
})
.unwrap();

Expand Down

0 comments on commit cff6145

Please sign in to comment.