From 7c9bd9c81af7f5badcde8a88f710c453db9e413b Mon Sep 17 00:00:00 2001 From: kitsunecafe Date: Sun, 6 Oct 2024 22:35:48 -0500 Subject: [PATCH] initial --- Cargo.toml | 6 ++++++ src/lib.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5905bc4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "maybe_owned" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ee6f8bc --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,22 @@ +pub enum MaybeOwned<'a, T: 'a> { + Owned(T), + Borrowed(&'a T), +} + +impl<'a, T: 'a> From for MaybeOwned<'a, T> { + fn from(value: T) -> Self { + Self::Owned(value) + } +} + +impl<'a, T: 'a> From<&'a T> for MaybeOwned<'a, T> { + fn from(value: &'a T) -> Self { + Self::Borrowed(value) + } +} + +impl<'a, T: 'a + Default> Default for MaybeOwned<'a, T> { + fn default() -> Self { + MaybeOwned::Owned(T::default()) + } +}