commit 7c9bd9c81af7f5badcde8a88f710c453db9e413b Author: kitsunecafe Date: Sun Oct 6 22:35:48 2024 -0500 initial 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()) + } +}