Skip to content

Commit

Permalink
refactor(iroh)!: Use ref-cast instead of fields to get the subsystem …
Browse files Browse the repository at this point in the history
…clients (#2374)

## Description

This is part 2 of replacing the fields in the iroh client with accessors
and avoiding having multiple copies of the rpc client handle.

## Breaking Changes

Removes the fields of iroh client. They were already marked as
deprecated in the last release.

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [x] Self-review.
- [x] Documentation updates if relevant.
- [ ] ~~Tests if relevant.~~
- [x] All breaking changes documented.
  • Loading branch information
rklaehn authored Jun 17, 2024
1 parent ac72938 commit be3e16e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ walkdir = "2"
# Examples
clap = { version = "4", features = ["derive"], optional = true }
indicatif = { version = "0.17", features = ["tokio"], optional = true }
ref-cast = "1.0.23"

[features]
default = ["metrics", "fs-store"]
Expand Down
43 changes: 10 additions & 33 deletions iroh/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use futures_lite::{Stream, StreamExt};
use quic_rpc::{RpcClient, ServiceConnection};
use ref_cast::RefCast;

#[doc(inline)]
pub use crate::rpc_protocol::RpcService;
Expand All @@ -25,19 +26,6 @@ mod node;
/// Iroh client.
#[derive(Debug, Clone)]
pub struct Iroh<C> {
/// Client for blobs operations.
#[deprecated(note = "Use `blobs` method instead", since = "0.18.0")]
pub blobs: blobs::Client<C>,
/// Client for docs operations.
#[deprecated(note = "Use `docs` method instead", since = "0.18.0")]
pub docs: docs::Client<C>,
/// Client for author operations.
#[deprecated(note = "Use `authors` method instead", since = "0.18.0")]
pub authors: authors::Client<C>,
/// Client for tags operations.
#[deprecated(note = "Use `tags` method instead", since = "0.18.0")]
pub tags: tags::Client<C>,

rpc: RpcClient<RpcService, C>,
}

Expand All @@ -47,38 +35,27 @@ where
{
/// Create a new high-level client to a Iroh node from the low-level RPC client.
pub fn new(rpc: RpcClient<RpcService, C>) -> Self {
#[allow(deprecated)]
Self {
blobs: blobs::Client { rpc: rpc.clone() },
docs: docs::Client { rpc: rpc.clone() },
authors: authors::Client { rpc: rpc.clone() },
tags: tags::Client { rpc: rpc.clone() },
rpc,
}
Self { rpc }
}

/// Client for blobs operations.
/// Blobs client
pub fn blobs(&self) -> &blobs::Client<C> {
#[allow(deprecated)]
&self.blobs
blobs::Client::ref_cast(&self.rpc)
}

/// Client for docs operations.
/// Docs client
pub fn docs(&self) -> &docs::Client<C> {
#[allow(deprecated)]
&self.docs
docs::Client::ref_cast(&self.rpc)
}

/// Client for author operations.
/// Authors client
pub fn authors(&self) -> &authors::Client<C> {
#[allow(deprecated)]
&self.authors
authors::Client::ref_cast(&self.rpc)
}

/// Client for tags operations.
/// Tags client
pub fn tags(&self) -> &tags::Client<C> {
#[allow(deprecated)]
&self.tags
tags::Client::ref_cast(&self.rpc)
}
}

Expand Down
4 changes: 3 additions & 1 deletion iroh/src/client/authors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use futures_lite::{stream::StreamExt, Stream};
use iroh_docs::{Author, AuthorId};
use quic_rpc::{RpcClient, ServiceConnection};
use ref_cast::RefCast;

use crate::rpc_protocol::{
AuthorCreateRequest, AuthorDeleteRequest, AuthorExportRequest, AuthorGetDefaultRequest,
Expand All @@ -13,7 +14,8 @@ use crate::rpc_protocol::{
use super::flatten;

/// Iroh authors client.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, RefCast)]
#[repr(transparent)]
pub struct Client<C> {
pub(super) rpc: RpcClient<RpcService, C>,
}
Expand Down
4 changes: 3 additions & 1 deletion iroh/src/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use iroh_blobs::{
use iroh_net::NodeAddr;
use portable_atomic::{AtomicU64, Ordering};
use quic_rpc::{client::BoxStreamSync, RpcClient, ServiceConnection};
use ref_cast::RefCast;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf};
use tokio_util::io::{ReaderStream, StreamReader};
Expand All @@ -40,7 +41,8 @@ use crate::rpc_protocol::{
use super::{flatten, tags, Iroh};

/// Iroh blobs client.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, RefCast)]
#[repr(transparent)]
pub struct Client<C> {
pub(super) rpc: RpcClient<RpcService, C>,
}
Expand Down
4 changes: 3 additions & 1 deletion iroh/src/client/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use iroh_docs::{
use iroh_net::NodeAddr;
use portable_atomic::{AtomicBool, Ordering};
use quic_rpc::{message::RpcMsg, RpcClient, ServiceConnection};
use ref_cast::RefCast;
use serde::{Deserialize, Serialize};

use crate::rpc_protocol::{
Expand All @@ -38,7 +39,8 @@ pub use iroh_docs::engine::{Origin, SyncEvent, SyncReason};
use super::{blobs, flatten};

/// Iroh docs client.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, RefCast)]
#[repr(transparent)]
pub struct Client<C> {
pub(super) rpc: RpcClient<RpcService, C>,
}
Expand Down
4 changes: 3 additions & 1 deletion iroh/src/client/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use anyhow::Result;
use futures_lite::{Stream, StreamExt};
use iroh_blobs::{BlobFormat, Hash, Tag};
use quic_rpc::{RpcClient, ServiceConnection};
use ref_cast::RefCast;
use serde::{Deserialize, Serialize};

use crate::rpc_protocol::{DeleteTagRequest, ListTagsRequest, RpcService};

/// Iroh tags client.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, RefCast)]
#[repr(transparent)]
pub struct Client<C> {
pub(super) rpc: RpcClient<RpcService, C>,
}
Expand Down

0 comments on commit be3e16e

Please sign in to comment.