refactor: LocalShared -> Forkable

This commit is contained in:
Shunsuke Shibayama 2023-06-18 18:30:09 +09:00
parent 75f4c206f6
commit 21c53dacc1
2 changed files with 11 additions and 11 deletions

View file

@ -270,18 +270,18 @@ impl<T: Clone> Shared<T> {
/// The initial value can be shared globally, but the changes are not reflected in other threads.
/// Otherwise, this behaves as a `RefCell`.
#[derive(Clone)]
pub struct LocalShared<T: Send + Clone> {
pub struct Forkable<T: Send + Clone> {
data: Arc<ThreadLocal<RefCell<T>>>,
init: Arc<T>,
}
impl<T: fmt::Debug + Send + Clone> fmt::Debug for LocalShared<T> {
impl<T: fmt::Debug + Send + Clone> fmt::Debug for Forkable<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.deref().fmt(f)
}
}
impl<T: fmt::Display + Send + Clone> fmt::Display for LocalShared<T>
impl<T: fmt::Display + Send + Clone> fmt::Display for Forkable<T>
where
RefCell<T>: fmt::Display,
{
@ -290,7 +290,7 @@ where
}
}
impl<T: Send + Clone> Deref for LocalShared<T> {
impl<T: Send + Clone> Deref for Forkable<T> {
type Target = RefCell<T>;
fn deref(&self) -> &Self::Target {
self.data
@ -298,7 +298,7 @@ impl<T: Send + Clone> Deref for LocalShared<T> {
}
}
impl<T: Send + Clone> LocalShared<T> {
impl<T: Send + Clone> Forkable<T> {
pub fn new(init: T) -> Self {
Self {
data: Arc::new(ThreadLocal::new()),

View file

@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
use std::mem;
use std::sync::atomic::AtomicUsize;
use erg_common::shared::LocalShared;
use erg_common::shared::Forkable;
use erg_common::traits::{LimitedDisplay, StructuralEq};
use erg_common::Str;
use erg_common::{addr_eq, log};
@ -491,7 +491,7 @@ impl<T> FreeKind<T> {
}
#[derive(Debug, Clone)]
pub struct Free<T: Send + Clone>(LocalShared<FreeKind<T>>);
pub struct Free<T: Send + Clone>(Forkable<FreeKind<T>>);
impl Hash for Free<Type> {
fn hash<H: Hasher>(&self, state: &mut H) {
@ -745,12 +745,12 @@ impl HasLevel for Free<TyParam> {
impl<T: Send + Clone> Free<T> {
pub fn new(f: FreeKind<T>) -> Self {
Self(LocalShared::new(f))
Self(Forkable::new(f))
}
pub fn new_unbound(level: Level, constraint: Constraint) -> Self {
UNBOUND_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Self(LocalShared::new(FreeKind::unbound(
Self(Forkable::new(FreeKind::unbound(
UNBOUND_ID.load(std::sync::atomic::Ordering::SeqCst),
level,
constraint,
@ -758,13 +758,13 @@ impl<T: Send + Clone> Free<T> {
}
pub fn new_named_unbound(name: Str, level: Level, constraint: Constraint) -> Self {
Self(LocalShared::new(FreeKind::named_unbound(
Self(Forkable::new(FreeKind::named_unbound(
name, level, constraint,
)))
}
pub fn new_linked(t: T) -> Self {
Self(LocalShared::new(FreeKind::Linked(t)))
Self(Forkable::new(FreeKind::Linked(t)))
}
/// returns linked type (panic if self is unbounded)