-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit before trying to use UniqueRc
- Loading branch information
Showing
10 changed files
with
195 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::{rc::Rc, ops::{DerefMut, Deref}, cell::RefCell}; | ||
|
||
struct UniqueRc<T: ?Sized>(Rc<T>); | ||
impl<T: ?Sized> UniqueRc<T> { | ||
pub fn into_rc(self) -> Rc<T> { | ||
self.0 | ||
} | ||
} | ||
impl<T> UniqueRc<T> { | ||
pub fn into_inner(self) -> T { | ||
// SAFETY: There is only ever one owner of Rc | ||
unsafe { Rc::try_unwrap(self.0).unwrap_unchecked() } | ||
} | ||
} | ||
|
||
impl<T> Deref for UniqueRc<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
self.0.as_ref() | ||
} | ||
} | ||
impl<T> DerefMut for UniqueRc<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
// SAFETY: There is only ever one owner of Rc | ||
unsafe { Rc::get_mut(&mut self.0).unwrap_unchecked() } | ||
} | ||
} |
Oops, something went wrong.