Skip to content

Instantly share code, notes, and snippets.

@demurgos
Created March 8, 2018 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demurgos/2cbdaed9100a26bf65d7af615a9f6324 to your computer and use it in GitHub Desktop.
Save demurgos/2cbdaed9100a26bf65d7af615a9f6324 to your computer and use it in GitHub Desktop.
use ::std::cell::Cell;
use ::std::fmt::Debug;
use ::std::ptr::NonNull;
// A node in my forward linked-list
pub struct GcBox<T: Debug + ?Sized> {
counter: Cell<usize>,
next: Option<NonNull<GcBox<Debug>>>,
value: T,
}
impl<T: Debug + ?Sized> GcBox<T> {
pub fn inc(&self) {
self.counter.set(self.counter.get() + 1);
}
}
// Start of the list
pub struct Root {
head: Option<NonNull<GcBox<Debug>>>,
}
fn main() {
let hello = String::from("hello");
{
let mut rt = Root { head: None };
let hello_box = GcBox { counter: Cell::new(0), next: None, value: &hello };
hello_box.inc();
rt.head = Some(NonNull::new(Box::into_raw(Box::new(hello_box))).unwrap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment