Created
March 8, 2018 00:16
-
-
Save demurgos/2cbdaed9100a26bf65d7af615a9f6324 to your computer and use it in GitHub Desktop.
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
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