Skip to content

Commit

Permalink
Rename AsSubject to ToSubject
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <[email protected]>
  • Loading branch information
Jarema committed Nov 1, 2023
1 parent 3691768 commit 3fbf7c8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
42 changes: 21 additions & 21 deletions async-nats/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.

use crate::connection::State;
use crate::subject::AsSubject;
use crate::subject::ToSubject;
use crate::ServerInfo;

use super::{header::HeaderMap, status::StatusCode, Command, Message, Subscriber};
Expand Down Expand Up @@ -148,12 +148,12 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn publish<S: AsSubject>(
pub async fn publish<S: ToSubject>(
&self,
subject: S,
payload: Bytes,
) -> Result<(), PublishError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

self.sender
.send(Command::Publish {
Expand Down Expand Up @@ -185,13 +185,13 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn publish_with_headers<S: AsSubject>(
pub async fn publish_with_headers<S: ToSubject>(
&self,
subject: S,
headers: HeaderMap,
payload: Bytes,
) -> Result<(), PublishError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

self.sender
.send(Command::Publish {
Expand Down Expand Up @@ -220,14 +220,14 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn publish_with_reply<S: AsSubject, R: AsSubject>(
pub async fn publish_with_reply<S: ToSubject, R: ToSubject>(
&self,
subject: S,
reply: R,
payload: Bytes,
) -> Result<(), PublishError> {
let subject = subject.as_subject();
let reply = reply.as_subject();
let subject = subject.to_subject();
let reply = reply.to_subject();

self.sender
.send(Command::Publish {
Expand Down Expand Up @@ -258,15 +258,15 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn publish_with_reply_and_headers<S: AsSubject, R: AsSubject>(
pub async fn publish_with_reply_and_headers<S: ToSubject, R: ToSubject>(
&self,
subject: S,
reply: R,
headers: HeaderMap,
payload: Bytes,
) -> Result<(), PublishError> {
let subject = subject.as_subject();
let reply = reply.as_subject();
let subject = subject.to_subject();
let reply = reply.to_subject();

self.sender
.send(Command::Publish {
Expand All @@ -290,12 +290,12 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn request<S: AsSubject>(
pub async fn request<S: ToSubject>(
&self,
subject: S,
payload: Bytes,
) -> Result<Message, RequestError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

trace!(
"request sent to subject: {} ({})",
Expand All @@ -321,13 +321,13 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn request_with_headers<S: AsSubject>(
pub async fn request_with_headers<S: ToSubject>(
&self,
subject: S,
headers: HeaderMap,
payload: Bytes,
) -> Result<Message, RequestError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

let request = Request::new().headers(headers).payload(payload);
self.send_request(subject, request).await
Expand All @@ -346,12 +346,12 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn send_request<S: AsSubject>(
pub async fn send_request<S: ToSubject>(
&self,
subject: S,
request: Request,
) -> Result<Message, RequestError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

if let Some(inbox) = request.inbox {
let timeout = request.timeout.unwrap_or(self.request_timeout);
Expand Down Expand Up @@ -463,8 +463,8 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn subscribe<S: AsSubject>(&self, subject: S) -> Result<Subscriber, SubscribeError> {
let subject = subject.as_subject();
pub async fn subscribe<S: ToSubject>(&self, subject: S) -> Result<Subscriber, SubscribeError> {
let subject = subject.to_subject();
let sid = self.next_subscription_id.fetch_add(1, Ordering::Relaxed);
let (sender, receiver) = mpsc::channel(self.subscription_capacity);

Expand Down Expand Up @@ -496,12 +496,12 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn queue_subscribe<S: AsSubject>(
pub async fn queue_subscribe<S: ToSubject>(
&self,
subject: S,
queue_group: String,
) -> Result<Subscriber, SubscribeError> {
let subject = subject.as_subject();
let subject = subject.to_subject();

let sid = self.next_subscription_id.fetch_add(1, Ordering::Relaxed);
let (sender, receiver) = mpsc::channel(self.subscription_capacity);
Expand Down
14 changes: 7 additions & 7 deletions async-nats/src/jetstream/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::header::{IntoHeaderName, IntoHeaderValue};
use crate::jetstream::account::Account;
use crate::jetstream::publish::PublishAck;
use crate::jetstream::response::Response;
use crate::subject::AsSubject;
use crate::subject::ToSubject;
use crate::{header, Client, Command, HeaderMap, HeaderValue, Message, StatusCode};
use bytes::Bytes;
use futures::future::BoxFuture;
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub async fn publish<S: AsSubject>(
pub async fn publish<S: ToSubject>(
&self,
subject: S,
payload: Bytes,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub async fn publish_with_headers<S: AsSubject>(
pub async fn publish_with_headers<S: ToSubject>(
&self,
subject: S,
headers: crate::header::HeaderMap,
Expand Down Expand Up @@ -182,12 +182,12 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub async fn send_publish<S: AsSubject>(
pub async fn send_publish<S: ToSubject>(
&self,
subject: S,
publish: Publish,
) -> Result<PublishAckFuture, PublishError> {
let subject = subject.as_subject();
let subject = subject.to_subject();
let (sender, receiver) = oneshot::channel();

let respond = self.client.new_inbox().into();
Expand Down Expand Up @@ -805,11 +805,11 @@ impl Context {
/// ```
pub async fn request<S, T, V>(&self, subject: S, payload: &T) -> Result<V, RequestError>
where
S: AsSubject,
S: ToSubject,
T: ?Sized + Serialize,
V: DeserializeOwned,
{
let subject = subject.as_subject();
let subject = subject.to_subject();
let request = serde_json::to_vec(&payload)
.map(Bytes::from)
.map_err(|err| RequestError::with_source(RequestErrorKind::Other, err))?;
Expand Down
16 changes: 8 additions & 8 deletions async-nats/src/subject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,24 @@ impl fmt::Display for Subject {
}
}

pub trait AsSubject {
fn as_subject(&self) -> Subject;
pub trait ToSubject {
fn to_subject(&self) -> Subject;
}

impl AsSubject for Subject {
fn as_subject(&self) -> Subject {
impl ToSubject for Subject {
fn to_subject(&self) -> Subject {
self.to_owned()
}
}

impl AsSubject for &'static str {
fn as_subject(&self) -> Subject {
impl ToSubject for &'static str {
fn to_subject(&self) -> Subject {
Subject::from_static(self)
}
}

impl AsSubject for String {
fn as_subject(&self) -> Subject {
impl ToSubject for String {
fn to_subject(&self) -> Subject {
Subject::from(self.as_str())
}
}

0 comments on commit 3fbf7c8

Please sign in to comment.