Skip to content

Commit

Permalink
der: document RefToOwned and OwnedToRef (#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo authored Sep 5, 2024
1 parent 074edb4 commit 3fb883b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions der/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@
//! # }
//! ```
//!
//! ## Owned vs Borrowed
//!
//! This crate exposes owned and borrowed objects for representing the same structure.
//!
//! [`RefToOwned`] and [`OwnedToRef`] are provided to convert objects from one to the other.
//!
//! # See also
//! For more information about ASN.1 DER we recommend the following guides:
//!
Expand Down Expand Up @@ -322,6 +328,8 @@
//! [`UintRef`]: asn1::UintRef
//! [`UtcTime`]: asn1::UtcTime
//! [`Utf8StringRef`]: asn1::Utf8StringRef
//! [`RefToOwned`]: referenced::RefToOwned
//! [`OwnedToRef`]: referenced::OwnedToRef
#[cfg(feature = "alloc")]
#[allow(unused_imports)]
Expand Down
12 changes: 11 additions & 1 deletion der/src/referenced.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! A module for working with referenced data.
/// A trait for borrowing data from an owned struct
///
/// This converts an object owning the data to one that will borrowing the content.
/// The newly created object lifetime will be tied to the object owning the data.
///
/// This is similar to [`alloc::borrow::Borrow`] or [`core::convert::AsRef`] but this returns
/// an owned structure that references directly the backing slices instead of borrowing
/// the whole structure.
pub trait OwnedToRef {
/// The resulting type referencing back to Self
type Borrowed<'a>
Expand All @@ -13,7 +20,10 @@ pub trait OwnedToRef {

/// A trait for cloning a referenced structure and getting owned objects
///
/// This is the pendant to [`OwnedToRef`]
/// This is the pendant to [`OwnedToRef`].
///
/// This converts an object borrowing data to one that will copy the data over and
/// own the content.
pub trait RefToOwned<'a> {
/// The resulting type after obtaining ownership.
type Owned: OwnedToRef<Borrowed<'a> = Self>
Expand Down
4 changes: 4 additions & 0 deletions spki/src/spki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ use crate::{fingerprint, FingerprintBytes};
use der::pem::PemLabel;

/// [`SubjectPublicKeyInfo`] with [`AnyRef`] algorithm parameters, and [`BitStringRef`] params.
///
/// This is the borrowing-pendant to [`SubjectPublicKeyInfoOwned`].
pub type SubjectPublicKeyInfoRef<'a> = SubjectPublicKeyInfo<AnyRef<'a>, BitStringRef<'a>>;

/// [`SubjectPublicKeyInfo`] with [`Any`] algorithm parameters, and [`BitString`] params.
///
/// This is the owning-pendant to [`SubjectPublicKeyInfoRef`].
#[cfg(feature = "alloc")]
pub type SubjectPublicKeyInfoOwned = SubjectPublicKeyInfo<Any, BitString>;

Expand Down

0 comments on commit 3fb883b

Please sign in to comment.