Skip to content

Commit

Permalink
Implement Ord & Eq for X509 and X509Ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Skepfyr committed May 24, 2022
1 parent af92501 commit 1147ed0
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use cfg_if::cfg_if;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_int, c_long};
use std::cmp;
use std::error::Error;
use std::ffi::{CStr, CString};
use std::fmt;
Expand Down Expand Up @@ -600,6 +601,29 @@ impl ToOwned for X509Ref {
}
}

impl Ord for X509Ref {
fn cmp(&self, other: &Self) -> cmp::Ordering {
// X509_cmp returns a number <0 for less than, 0 for equal and >0 for greater than.
// It can't fail if both pointers are valid, which we know is true.
let cmp = unsafe { ffi::X509_cmp(self.as_ptr(), other.as_ptr()) };
cmp.cmp(&0)
}
}

impl PartialOrd for X509Ref {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl PartialEq for X509Ref {
fn eq(&self, other: &Self) -> bool {
self.cmp(other).is_eq()
}
}

impl Eq for X509Ref {}

impl X509 {
/// Returns a new builder.
pub fn builder() -> Result<X509Builder, ErrorStack> {
Expand Down Expand Up @@ -700,6 +724,26 @@ impl Stackable for X509 {
type StackType = ffi::stack_st_X509;
}

impl Ord for X509 {
fn cmp(&self, other: &Self) -> cmp::Ordering {
X509Ref::cmp(self, other)
}
}

impl PartialOrd for X509 {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
X509Ref::partial_cmp(self, other)
}
}

impl PartialEq for X509 {
fn eq(&self, other: &Self) -> bool {
X509Ref::eq(self, other)
}
}

impl Eq for X509 {}

/// A context object required to construct certain `X509` extension values.
pub struct X509v3Context<'a>(ffi::X509V3_CTX, PhantomData<(&'a X509Ref, &'a ConfRef)>);

Expand Down

0 comments on commit 1147ed0

Please sign in to comment.