Skip to content

Commit

Permalink
Create a general set function for Rt<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
CeleritasCelery committed Dec 18, 2023
1 parent 213a21b commit 7c46716
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/core/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl RootedEnv {
// we will bind it to the new value
for binding in &mut *self.binding_stack {
if binding.0 == var && binding.1.is_none() {
binding.1.set(value);
binding.1.set(Some(value));
}
}
Ok(())
Expand Down
25 changes: 6 additions & 19 deletions src/core/gc/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ impl<T> Rt<T> {
{
unsafe { &*(slice as *const [Rt<T>] as *const [U]) }
}

pub(crate) fn set<U: IntoRoot<T>>(&mut self, item: U) {
// SAFETY: we drop the old type so it never exposed and take the new
// rooted type and replace it.
unsafe { self.inner = item.into_root() }
}
}

impl TryFrom<&Rt<GcObj<'_>>> for usize {
Expand Down Expand Up @@ -351,15 +357,6 @@ impl<T> Rt<Gc<T>> {
let gc: Gc<U> = self.bind(cx);
gc.untag_erased()
}

pub(crate) fn set<U>(&mut self, item: U)
where
U: IntoRoot<Gc<T>>,
{
unsafe {
self.inner = item.into_root();
}
}
}

impl From<&Rt<GcObj<'_>>> for Option<()> {
Expand Down Expand Up @@ -399,10 +396,6 @@ impl IntoObject for &mut Rt<GcObj<'static>> {
}

impl Rt<&Cons> {
pub(crate) fn set(&mut self, item: &Cons) {
self.inner = unsafe { std::mem::transmute(item) }
}

pub(crate) fn car<'ob>(&self, cx: &'ob Context) -> GcObj<'ob> {
self.bind(cx).car()
}
Expand Down Expand Up @@ -437,12 +430,6 @@ impl<T> Deref for Rt<Option<T>> {
}

impl<T> Rt<Option<T>> {
pub(crate) fn set<U: IntoRoot<T>>(&mut self, obj: U) {
unsafe {
self.inner = Some(obj.into_root());
}
}

// This is not really dead code, but the static analysis fails to find it
#[allow(dead_code)]
pub(crate) fn as_ref(&self) -> Option<&Rt<T>> {
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Interpreter<'_> {
let value = self.eval_form(form, cx)?;
count += 1;
if prog_num == count {
returned_form.set(value);
returned_form.set(Some(value));
}
}
match &**returned_form {
Expand Down

0 comments on commit 7c46716

Please sign in to comment.