Skip to content

Commit

Permalink
Improve ergonomics of links
Browse files Browse the repository at this point in the history
Before this change, users were forced to pass `info` field
from `Object`. This is not very ergonomic, as what library
needs to add link - a whole [Object], or just [ObjectInfo]
should be internal. This change adds a trait that handles that.

Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Aug 18, 2023
1 parent fe5b542 commit 8365b91
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions async-nats/src/jetstream/object_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,16 @@ impl ObjectStore {
/// let jetstream = async_nats::jetstream::new(client);
/// let bucket = jetstream.get_object_store("bucket").await?;
/// let object = bucket.get("object").await?;
/// bucket.add_link("link_to_object", &object.info).await?;
/// bucket.add_link("link_to_object", &object).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_link<T: ToString>(
&self,
name: T,
object: &ObjectInfo,
) -> Result<ObjectInfo, AddLinkError> {
pub async fn add_link<'a, T, O>(&self, name: T, object: O) -> Result<ObjectInfo, AddLinkError>
where
T: ToString,
O: AsObjectInfo,
{
let object = object.as_info();
let name = name.to_string();
if name.is_empty() {
return Err(AddLinkError::new(AddLinkErrorKind::EmptyName));
Expand Down Expand Up @@ -714,16 +715,18 @@ impl ObjectStore {
/// # async fn main() -> Result<(), async_nats::Error> {
/// use async_nats::jetstream::object_store;
/// let client = async_nats::connect("demo.nats.io").await?;
/// let jetstream = async_nats::jetstream::new(client);
/// let jetstream = async_nats::jetstream::new(client);
/// let bucket = jetstream.get_object_store("bucket").await?;
/// bucket.add_bucket_link("link_to_object", "another_bucket").await?;
/// bucket
/// .add_bucket_link("link_to_object", "another_bucket")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_bucket_link<T: ToString>(
pub async fn add_bucket_link<T: ToString, U: ToString>(
&self,
name: T,
bucket: T,
bucket: U,
) -> Result<ObjectInfo, AddLinkError> {
let name = name.to_string();
let bucket = bucket.to_string();
Expand Down Expand Up @@ -1086,6 +1089,21 @@ impl From<&str> for ObjectMeta {
}
}

pub trait AsObjectInfo {
fn as_info(&self) -> &ObjectInfo;
}

impl AsObjectInfo for &Object<'_> {
fn as_info(&self) -> &ObjectInfo {
&self.info
}
}
impl AsObjectInfo for &ObjectInfo {
fn as_info(&self) -> &ObjectInfo {
self
}
}

impl From<ObjectInfo> for ObjectMeta {
fn from(info: ObjectInfo) -> Self {
ObjectMeta {
Expand Down

0 comments on commit 8365b91

Please sign in to comment.