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