Skip to content

Commit

Permalink
const-oid: factor traits into module (#1301)
Browse files Browse the repository at this point in the history
Adds a `traits` module containing `AssociatedOid` and `DynAssociatedOid`
  • Loading branch information
tarcieri authored Jan 5, 2024
1 parent 2bdc827 commit 26a1a02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
24 changes: 2 additions & 22 deletions const-oid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod buffer;
mod encoder;
mod error;
mod parser;
mod traits;

#[cfg(feature = "db")]
pub mod db;
Expand All @@ -38,6 +39,7 @@ pub use crate::{
arcs::{Arc, Arcs},
buffer::Buffer,
error::{Error, Result},
traits::{AssociatedOid, DynAssociatedOid},
};

use crate::encoder::Encoder;
Expand All @@ -48,28 +50,6 @@ use core::{fmt, str::FromStr};
/// Makes `ObjectIdentifier` 40-bytes total w\ 1-byte length.
const DEFAULT_MAX_SIZE: usize = 39;

/// A trait which associates an OID with a type.
pub trait AssociatedOid {
/// The OID associated with this type.
const OID: ObjectIdentifier;
}

/// A trait which associates a dynamic, `&self`-dependent OID with a type,
/// which may change depending on the type's value.
///
/// This trait is object safe and auto-impl'd for any types which impl
/// [`AssociatedOid`].
pub trait DynAssociatedOid {
/// Get the OID associated with this value.
fn oid(&self) -> ObjectIdentifier;
}

impl<T: AssociatedOid> DynAssociatedOid for T {
fn oid(&self) -> ObjectIdentifier {
T::OID
}
}

/// Object identifier (OID).
///
/// OIDs are hierarchical structures consisting of "arcs", i.e. integer
Expand Down
25 changes: 25 additions & 0 deletions const-oid/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Trait definitions.
use crate::ObjectIdentifier;

/// A trait which associates an OID with a type.
pub trait AssociatedOid {
/// The OID associated with this type.
const OID: ObjectIdentifier;
}

/// A trait which associates a dynamic, `&self`-dependent OID with a type,
/// which may change depending on the type's value.
///
/// This trait is object safe and auto-impl'd for any types which impl
/// [`AssociatedOid`].
pub trait DynAssociatedOid {
/// Get the OID associated with this value.
fn oid(&self) -> ObjectIdentifier;
}

impl<T: AssociatedOid> DynAssociatedOid for T {
fn oid(&self) -> ObjectIdentifier {
T::OID
}
}

0 comments on commit 26a1a02

Please sign in to comment.