Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- trait DataContainer<'a, T> {
- fn as_ref(&self) -> &T;
- fn to_owned(&self) -> Self
- where
- T: Clone;
- }
- enum StringOrRef<'a> {
- Owned(String),
- Borrowed(&'a str),
- }
- impl<'a> DataContainer<'a, str> for StringOrRef<'a> {
- fn as_ref(&self) -> &str {
- match self {
- StringOrRef::Owned(s) => s.as_str(),
- StringOrRef::Borrowed(s) => s,
- }
- }
- fn to_owned(&self) -> StringOrRef<'static> {
- match self {
- StringOrRef::Owned(s) => StringOrRef::Owned(s.clone()),
- StringOrRef::Borrowed(s) => StringOrRef::Owned(s.to_owned()),
- }
- }
- }
- enum DataOrRef<'a, T> {
- Owned(T),
- Borrowed(&'a T),
- }
- impl<'a, T: Clone> DataContainer<'a, T> for DataOrRef<'a, T> {
- fn as_ref(&self) -> &T {
- match self {
- DataOrRef::Owned(d) => d,
- DataOrRef::Borrowed(d) => d,
- }
- }
- fn to_owned(&self) -> DataOrRef<'static, T> {
- match self {
- DataOrRef::Owned(d) => DataOrRef::Owned(d.clone()),
- DataOrRef::Borrowed(d) => DataOrRef::Owned(d.clone()),
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement