Skip to content

Commit

Permalink
Implement &str-like PartialEq for Utf8Payload
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic authored and daniel-abramov committed Dec 16, 2024
1 parent b0d74ae commit ff7325f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
- Implement `AsRef<[u8]>` for `Payload`, `AsRef<[u8]>`, `AsRef<str>` for `Utf8Payload`.
- Implement `&str`-like `PartialEq` for `Utf8Payload`.

# 0.25.0

Expand Down
18 changes: 18 additions & 0 deletions src/protocol/frame/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ impl AsRef<[u8]> for Utf8Payload {
}
}

impl<T> PartialEq<T> for Utf8Payload
where
for<'a> &'a str: PartialEq<T>,
{
/// ```
/// use tungstenite::protocol::frame::Utf8Payload;
/// let payload = Utf8Payload::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
}
}

/// A payload of a WebSocket frame.
#[derive(Debug, Clone)]
pub enum Payload {
Expand Down

0 comments on commit ff7325f

Please sign in to comment.