Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove the cow #34

Merged
merged 4 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ where
}
}

impl<Params> Request<&Params>
where
Params: Clone,
{
/// Clone the request, including the request parameters.
pub fn into_owned_params(self) -> Request<Params> {
Request { meta: self.meta, params: self.params.clone() }
}
}

impl<'a, Params> Request<Params>
where
Params: AsRef<RawValue> + 'a,
Expand Down
17 changes: 17 additions & 0 deletions crates/rpc-client/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ where
}
}

impl<Conn, Params, Resp> RpcCall<Conn, &Params, Resp>
where
Conn: Transport + Clone,
Params: RpcParam + Clone,
{
/// Convert this call into one with owned params, by cloning the params.
pub fn into_owned_params(self) -> RpcCall<Conn, Params, Resp> {
if let CallState::Prepared { request, connection } = self.state {
let request =
request.expect("No params in prepared. This is a bug").into_owned_params();
RpcCall::new(request, connection)
} else {
panic!("Cannot get params after request has been sent");
}
}
}

impl<'a, Conn, Params, Resp> RpcCall<Conn, Params, Resp>
where
Conn: Transport + Clone,
Expand Down
17 changes: 7 additions & 10 deletions crates/rpc-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::{BatchRequest, ClientBuilder, RpcCall};
use alloy_json_rpc::{Id, Request, RequestMeta, RpcParam, RpcReturn};
use alloy_transport::{BoxTransport, Transport, TransportConnect, TransportError};
use alloy_transport_http::Http;
use std::{
borrow::Cow,
sync::atomic::{AtomicU64, Ordering},
};
use std::sync::atomic::{AtomicU64, Ordering};
use tower::{layer::util::Identity, ServiceBuilder};

/// A JSON-RPC client.
Expand Down Expand Up @@ -57,11 +54,11 @@ impl<T> RpcClient<T> {
/// This function reserves an ID for the request, however the request
/// is not sent. To send a request, use [`RpcClient::prepare`] and await
/// the returned [`RpcCall`].
pub fn make_request<'a, Params: RpcParam>(
pub fn make_request<Params: RpcParam>(
&self,
method: &'static str,
params: Cow<'a, Params>,
) -> Request<Cow<'a, Params>> {
params: Params,
) -> Request<Params> {
Request { meta: RequestMeta { method, id: self.next_id() }, params }
}

Expand Down Expand Up @@ -108,11 +105,11 @@ where
/// Serialization is done lazily. It will not be performed until the call
/// is awaited. This means that if a serializer error occurs, it will not
/// be caught until the call is awaited.
pub fn prepare<'a, Params: RpcParam, Resp: RpcReturn>(
pub fn prepare<Params: RpcParam, Resp: RpcReturn>(
&self,
method: &'static str,
params: Cow<'a, Params>,
) -> RpcCall<T, Cow<'a, Params>, Resp> {
params: Params,
) -> RpcCall<T, Params, Resp> {
let request = self.make_request(method, params);
RpcCall::new(request, self.transport.clone())
}
Expand Down