Skip to content

Commit

Permalink
Add Utf8Bytes str-like PartialEq impls
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic authored and daniel-abramov committed Dec 17, 2024
1 parent 3e16b5b commit 6825fec
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/protocol/frame/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,44 @@ impl Utf8Bytes {
impl std::ops::Deref for Utf8Bytes {
type Target = str;

/// ```
/// use tungstenite::protocol::frame::Utf8Bytes;
///
/// /// Example fn that takes a str slice
/// fn a(s: &str) {}
///
/// let data = Utf8Bytes::from_static("foo123");
///
/// // auto-deref as arg
/// a(&data);
///
/// // deref to str methods
/// assert_eq!(data.len(), 6);
/// ```
#[inline]
fn deref(&self) -> &Self::Target {
self.as_str()
}
}

impl<T> PartialEq<T> for Utf8Bytes
where
for<'a> &'a str: PartialEq<T>,
{
/// ```
/// use tungstenite::protocol::frame::Utf8Bytes;
/// let payload = Utf8Bytes::from_static("foo123");
/// assert_eq!(payload, "foo123");
/// assert_eq!(payload, "foo123".to_string());
/// assert_eq!(payload, &"foo123".to_string());
/// assert_eq!(payload, std::borrow::Cow::from("foo123"));
/// ```
#[inline]
fn eq(&self, other: &T) -> bool {
self.as_str() == *other
}
}

impl Display for Utf8Bytes {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down

0 comments on commit 6825fec

Please sign in to comment.