Skip to content

Commit 1147ed0

Browse files
committed
Implement Ord & Eq for X509 and X509Ref
1 parent af92501 commit 1147ed0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

openssl/src/x509/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use cfg_if::cfg_if;
1111
use foreign_types::{ForeignType, ForeignTypeRef};
1212
use libc::{c_int, c_long};
13+
use std::cmp;
1314
use std::error::Error;
1415
use std::ffi::{CStr, CString};
1516
use std::fmt;
@@ -600,6 +601,29 @@ impl ToOwned for X509Ref {
600601
}
601602
}
602603

604+
impl Ord for X509Ref {
605+
fn cmp(&self, other: &Self) -> cmp::Ordering {
606+
// X509_cmp returns a number <0 for less than, 0 for equal and >0 for greater than.
607+
// It can't fail if both pointers are valid, which we know is true.
608+
let cmp = unsafe { ffi::X509_cmp(self.as_ptr(), other.as_ptr()) };
609+
cmp.cmp(&0)
610+
}
611+
}
612+
613+
impl PartialOrd for X509Ref {
614+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
615+
Some(self.cmp(other))
616+
}
617+
}
618+
619+
impl PartialEq for X509Ref {
620+
fn eq(&self, other: &Self) -> bool {
621+
self.cmp(other).is_eq()
622+
}
623+
}
624+
625+
impl Eq for X509Ref {}
626+
603627
impl X509 {
604628
/// Returns a new builder.
605629
pub fn builder() -> Result<X509Builder, ErrorStack> {
@@ -700,6 +724,26 @@ impl Stackable for X509 {
700724
type StackType = ffi::stack_st_X509;
701725
}
702726

727+
impl Ord for X509 {
728+
fn cmp(&self, other: &Self) -> cmp::Ordering {
729+
X509Ref::cmp(self, other)
730+
}
731+
}
732+
733+
impl PartialOrd for X509 {
734+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
735+
X509Ref::partial_cmp(self, other)
736+
}
737+
}
738+
739+
impl PartialEq for X509 {
740+
fn eq(&self, other: &Self) -> bool {
741+
X509Ref::eq(self, other)
742+
}
743+
}
744+
745+
impl Eq for X509 {}
746+
703747
/// A context object required to construct certain `X509` extension values.
704748
pub struct X509v3Context<'a>(ffi::X509V3_CTX, PhantomData<(&'a X509Ref, &'a ConfRef)>);
705749

0 commit comments

Comments
 (0)