Skip to content

Commit

Permalink
lib: impl PartialEq/Eq for BytesInner
Browse files Browse the repository at this point in the history
This allows deriving `PartialEq` and `Eq` automatically for the two
wrapper types, `Der` and `EchConfigListBytes`. The `BytesInner` type is
crate-internal and so we don't have to be worried about users
accidentally comparing `BytesInner` from a `EchConfigListBytes` with
a `Der` `BytesInner` without the broader type context catching the err.
  • Loading branch information
cpu committed Apr 18, 2024
1 parent 00df307 commit 14ed22c
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl CertificateDer<'_> {

/// A TLS-encoded Encrypted Client Hello (ECH) configuration list (`ECHConfigList`); as specified in
/// [draft-ietf-tls-esni-18 §4](https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#section-4)
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq)]
pub struct EchConfigListBytes<'a>(BytesInner<'a>);

impl EchConfigListBytes<'_> {
Expand All @@ -519,14 +519,6 @@ impl fmt::Debug for EchConfigListBytes<'_> {
}
}

impl PartialEq for EchConfigListBytes<'_> {
fn eq(&self, other: &Self) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl Eq for EchConfigListBytes<'_> {}

impl AsRef<[u8]> for EchConfigListBytes<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Expand Down Expand Up @@ -715,7 +707,7 @@ impl UnixTime {
/// This wrapper type is used to represent DER-encoded data in a way that is agnostic to whether
/// the data is owned (by a `Vec<u8>`) or borrowed (by a `&[u8]`). Support for the owned
/// variant is only available when the `alloc` feature is enabled.
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq)]
pub struct Der<'a>(BytesInner<'a>);

impl<'a> Der<'a> {
Expand Down Expand Up @@ -762,14 +754,6 @@ impl fmt::Debug for Der<'_> {
}
}

impl PartialEq for Der<'_> {
fn eq(&self, other: &Self) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl Eq for Der<'_> {}

#[derive(Clone)]
enum BytesInner<'a> {
#[cfg(feature = "alloc")]
Expand All @@ -787,6 +771,25 @@ impl BytesInner<'_> {
}
}

impl PartialEq for BytesInner<'_> {
fn eq(&self, other: &Self) -> bool {
match self {
#[cfg(feature = "alloc")]
Self::Owned(vec) => match other {
Self::Owned(other_vec) => vec == other_vec,
Self::Borrowed(other_slice) => vec == *other_slice,
},
Self::Borrowed(slice) => match other {
#[cfg(feature = "alloc")]
Self::Owned(other_vec) => *slice == other_vec,
Self::Borrowed(other_slice) => slice == other_slice,
},
}
}
}

impl Eq for BytesInner<'_> {}

// Format an iterator of u8 into a hex string
fn hex<'a>(f: &mut fmt::Formatter<'_>, payload: impl IntoIterator<Item = &'a u8>) -> fmt::Result {
for (i, b) in payload.into_iter().enumerate() {
Expand Down

0 comments on commit 14ed22c

Please sign in to comment.