Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const-oid: factor traits into module #1301

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}