This commit is contained in:
Rowan 2024-10-06 22:35:48 -05:00
commit 7c9bd9c81a
2 changed files with 28 additions and 0 deletions

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "maybe_owned"
version = "0.1.0"
edition = "2021"
[dependencies]

22
src/lib.rs Normal file
View file

@ -0,0 +1,22 @@
pub enum MaybeOwned<'a, T: 'a> {
Owned(T),
Borrowed(&'a T),
}
impl<'a, T: 'a> From<T> 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())
}
}