mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 10:49:54 +00:00
Rename dir: compiler
-> crates
This commit is contained in:
parent
e1004b538d
commit
a127564b31
221 changed files with 17 additions and 19 deletions
97
crates/erg_common/shared.rs
Normal file
97
crates/erg_common/shared.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Shared<T: ?Sized>(Rc<RefCell<T>>);
|
||||
|
||||
impl<T: PartialEq> PartialEq for Shared<T> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for Shared<T> {
|
||||
fn clone(&self) -> Shared<T> {
|
||||
Self(Rc::clone(&self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Eq> Eq for Shared<T> {}
|
||||
|
||||
impl<T: Hash> Hash for Shared<T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.borrow().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> Default for Shared<T> {
|
||||
fn default() -> Self {
|
||||
Self::new(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for Shared<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.borrow())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Shared<T> {
|
||||
pub fn new(t: T) -> Self {
|
||||
Self(Rc::new(RefCell::new(t)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> T {
|
||||
let refcell = match Rc::try_unwrap(self.0) {
|
||||
Ok(refcell) => refcell,
|
||||
Err(_rc) => panic!("unwrapping failed"),
|
||||
};
|
||||
RefCell::into_inner(refcell)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Shared<T> {
|
||||
#[inline]
|
||||
pub fn copy(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn borrow(&self) -> Ref<'_, T> {
|
||||
RefCell::borrow(&self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
||||
RefCell::borrow_mut(&self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_ptr(&self) -> *mut T {
|
||||
RefCell::as_ptr(&self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, std::cell::BorrowMutError> {
|
||||
RefCell::try_borrow_mut(&self.0)
|
||||
}
|
||||
|
||||
pub fn can_borrow(&self) -> bool {
|
||||
RefCell::try_borrow(&self.0).is_ok()
|
||||
}
|
||||
|
||||
pub fn can_borrow_mut(&self) -> bool {
|
||||
RefCell::try_borrow_mut(&self.0).is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Shared<T> {
|
||||
#[inline]
|
||||
pub fn clone_inner(&self) -> T {
|
||||
self.borrow().clone()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue