summaryrefslogtreecommitdiffstats
path: root/vendor/der/src/referenced.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/der/src/referenced.rs')
-rw-r--r--vendor/der/src/referenced.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/der/src/referenced.rs b/vendor/der/src/referenced.rs
new file mode 100644
index 000000000..bb920db3a
--- /dev/null
+++ b/vendor/der/src/referenced.rs
@@ -0,0 +1,47 @@
+//! A module for working with referenced data.
+
+/// A trait for borrowing data from an owned struct
+pub trait OwnedToRef {
+ /// The resulting type referencing back to Self
+ type Borrowed<'a>
+ where
+ Self: 'a;
+
+ /// Creates a new object referencing back to the self for storage
+ fn owned_to_ref(&self) -> Self::Borrowed<'_>;
+}
+
+/// A trait for cloning a referenced structure and getting owned objects
+///
+/// This is the pendant to [`OwnedToRef`]
+pub trait RefToOwned<'a> {
+ /// The resulting type after obtaining ownership.
+ type Owned: OwnedToRef<Borrowed<'a> = Self>
+ where
+ Self: 'a;
+
+ /// Creates a new object taking ownership of the data
+ fn ref_to_owned(&self) -> Self::Owned;
+}
+
+impl<T> OwnedToRef for Option<T>
+where
+ T: OwnedToRef,
+{
+ type Borrowed<'a> = Option<T::Borrowed<'a>> where T: 'a;
+
+ fn owned_to_ref(&self) -> Self::Borrowed<'_> {
+ self.as_ref().map(|o| o.owned_to_ref())
+ }
+}
+
+impl<'a, T> RefToOwned<'a> for Option<T>
+where
+ T: RefToOwned<'a> + 'a,
+ T::Owned: OwnedToRef,
+{
+ type Owned = Option<T::Owned>;
+ fn ref_to_owned(&self) -> Self::Owned {
+ self.as_ref().map(|o| o.ref_to_owned())
+ }
+}