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

feat(subject): Adds into_string method to Subject #1161

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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
19 changes: 19 additions & 0 deletions async-nats/src/subject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ impl Subject {
pub fn as_str(&self) -> &str {
self
}

/// Turns the `Subject` into a `String`, consuming it.
///
/// Note that this function is not implemented as `From<Subject> for String` as the conversion
/// from the underlying type could involve an allocation. If the `Subject` is owned data, it
/// will not allocate, but if it was constructed from borrowed data, it will.
///
/// # Examples
///
/// ```
/// use async_nats::Subject;
///
/// let s = Subject::from("foo");
/// let sub = s.into_string();
/// ```
pub fn into_string(self) -> String {
// SAFETY: We have guaranteed that the bytes in the `Subject` struct are valid UTF-8.
unsafe { String::from_utf8_unchecked(self.bytes.into()) }
}
}

impl<'a> From<&'a str> for Subject {
Expand Down