Skip to content

Commit

Permalink
der: impl RefToOwned/OwnedToRef for &[u8]/Box<[u8]>
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <baloo@superbaloo.net>
  • Loading branch information
baloo committed Aug 4, 2023
1 parent 3a7c698 commit ae56ced
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions der/src/referenced.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A module for working with referenced data.

use alloc::boxed::Box;

/// A trait for borrowing data from an owned struct
pub trait OwnedToRef {
/// The resulting type referencing back to Self
Expand Down Expand Up @@ -45,3 +47,19 @@ where
self.as_ref().map(|o| o.ref_to_owned())
}
}

impl<'a> RefToOwned<'a> for &'a [u8] {
type Owned = Box<[u8]>;

fn ref_to_owned(&self) -> Self::Owned {
Box::from(*self)
}
}

impl OwnedToRef for Box<[u8]> {
type Borrowed<'a> = &'a [u8];

fn owned_to_ref(&self) -> Self::Borrowed<'_> {
self.as_ref()
}
}

0 comments on commit ae56ced

Please sign in to comment.