From 26a1a02aef326c2441608ffb059e07f39cbafe98 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 4 Jan 2024 17:20:42 -0700 Subject: [PATCH] const-oid: factor traits into module (#1301) Adds a `traits` module containing `AssociatedOid` and `DynAssociatedOid` --- const-oid/src/lib.rs | 24 ++---------------------- const-oid/src/traits.rs | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 const-oid/src/traits.rs diff --git a/const-oid/src/lib.rs b/const-oid/src/lib.rs index 794daaa13..b966f2703 100644 --- a/const-oid/src/lib.rs +++ b/const-oid/src/lib.rs @@ -30,6 +30,7 @@ mod buffer; mod encoder; mod error; mod parser; +mod traits; #[cfg(feature = "db")] pub mod db; @@ -38,6 +39,7 @@ pub use crate::{ arcs::{Arc, Arcs}, buffer::Buffer, error::{Error, Result}, + traits::{AssociatedOid, DynAssociatedOid}, }; use crate::encoder::Encoder; @@ -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 DynAssociatedOid for T { - fn oid(&self) -> ObjectIdentifier { - T::OID - } -} - /// Object identifier (OID). /// /// OIDs are hierarchical structures consisting of "arcs", i.e. integer diff --git a/const-oid/src/traits.rs b/const-oid/src/traits.rs new file mode 100644 index 000000000..05b43617b --- /dev/null +++ b/const-oid/src/traits.rs @@ -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 DynAssociatedOid for T { + fn oid(&self) -> ObjectIdentifier { + T::OID + } +}