Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Feb 10, 2025
1 parent 0025583 commit 4845d92
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crates/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub mod layers;

mod provider;
pub use provider::{
builder, Caller, EthCall, EthCallMany, EthCallManyParams, EthCallParams, FilterPollerBuilder,
ParamsWithBlock, Provider, ProviderCall, RootProvider, RpcWithBlock, SendableTx, SendableTxErr,
WalletProvider, WrappedProvider,
builder, Caller, DynProvider, EthCall, EthCallMany, EthCallManyParams, EthCallParams,
FilterPollerBuilder, ParamsWithBlock, Provider, ProviderCall, RootProvider, RpcWithBlock,
SendableTx, SendableTxErr, WalletProvider,
};

pub mod utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ use std::{borrow::Cow, sync::Arc};
///
/// This type will delegate all functions to the wrapped provider, with the exception of non
/// object-safe functions (e.g. [`Provider::subscribe`]) which use the default trait implementation.
///
/// This is a convenience type for `Arc<dyn Provider<N> + 'static>`.
#[derive(Clone)]
pub struct WrappedProvider<N = Ethereum>(Arc<dyn Provider<N> + 'static>);
pub struct DynProvider<N = Ethereum>(Arc<dyn Provider<N> + 'static>);

impl<N: Network> WrappedProvider<N> {
/// Creates a new [`WrappedProvider`] by erasing the type.
impl<N: Network> DynProvider<N> {
/// Creates a new [`DynProvider`] by erasing the type.
pub fn new<P>(provider: P) -> Self
where
P: Provider<N> + 'static,
Expand All @@ -39,7 +41,7 @@ impl<N: Network> WrappedProvider<N> {

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N: Network> Provider<N> for WrappedProvider<N> {
impl<N: Network> Provider<N> for DynProvider<N> {
fn root(&self) -> &RootProvider<N> {
self.0.root()
}
Expand All @@ -53,7 +55,7 @@ impl<N: Network> Provider<N> for WrappedProvider<N> {
}

#[allow(clippy::use_self)]
fn wrapped(self) -> WrappedProvider<N>
fn erased(self) -> DynProvider<N>
where
Self: Sized + 'static,
{
Expand Down Expand Up @@ -411,7 +413,7 @@ impl<N: Network> Provider<N> for WrappedProvider<N> {
}
}

impl<N> std::fmt::Debug for WrappedProvider<N> {
impl<N> std::fmt::Debug for DynProvider<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("WrappedProvider").field(&"<dyn Provider>").finish()
}
Expand All @@ -426,7 +428,7 @@ mod tests {
#[test]
fn test_erased_provider() {
let provider =
ProviderBuilder::new().on_http("http://localhost:8080".parse().unwrap()).wrapped();
ProviderBuilder::new().on_http("http://localhost:8080".parse().unwrap()).erased();
assert_provider(provider);
}
}
4 changes: 2 additions & 2 deletions crates/provider/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub use wallet::WalletProvider;
mod with_block;
pub use with_block::{ParamsWithBlock, RpcWithBlock};

mod helper;
pub use helper::WrappedProvider;
mod erased;
pub use erased::DynProvider;
14 changes: 7 additions & 7 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use alloy_transport::TransportResult;
use serde_json::value::RawValue;
use std::borrow::Cow;

use super::{EthCallMany, WrappedProvider};
use super::{DynProvider, EthCallMany};

/// A task that polls the provider with `eth_getFilterChanges`, returning a list of `R`.
///
Expand Down Expand Up @@ -91,23 +91,23 @@ pub trait Provider<N: Network = Ethereum>: Send + Sync {
self.root().weak_client()
}

/// Returns a type erased provider wrapped in Arc [`super::WrappedProvider`].
/// Returns a type erased provider wrapped in Arc [`super::DynProvider`].
///
/// ```no_run
/// use alloy_provider::{Provider, ProviderBuilder, WrappedProvider};
/// use alloy_provider::{DynProvider, Provider, ProviderBuilder};
/// # async fn f() {
/// let provider: WrappedProvider =
/// ProviderBuilder::new().on_http("http://localhost:8080".parse().unwrap()).wrapped();
/// let provider: DynProvider =
/// ProviderBuilder::new().on_http("http://localhost:8080".parse().unwrap()).erased();
/// let block = provider.get_block_number().await.unwrap();
///
/// # }
/// ```
#[auto_impl(keep_default_for(&, &mut, Rc, Arc, Box))]
fn wrapped(self) -> WrappedProvider<N>
fn erased(self) -> DynProvider<N>
where
Self: Sized + 'static,
{
WrappedProvider::new(self)
DynProvider::new(self)
}

/// Gets the accounts in the remote node. This is usually empty unless you're using a local
Expand Down

0 comments on commit 4845d92

Please sign in to comment.