Skip to content

Instantly share code, notes, and snippets.

@jaheba
Created March 3, 2017 13:28
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 jaheba/0fbde0562b60112943e5dc75560ccd60 to your computer and use it in GitHub Desktop.
Save jaheba/0fbde0562b60112943e5dc75560ccd60 to your computer and use it in GitHub Desktop.
struct Item {
list: *mut List,
}
impl Item {
fn new(list: *mut List) -> Self {
Item { list: list }
}
}
impl Item {
fn get_len(&self) -> usize {
unsafe {
(*self.list).values.len()
}
}
}
struct List {
values: Vec<Item>,
}
impl List {
fn new() -> Self {
List { values: Vec::new() }
}
fn push(&mut self) -> &Item {
let item = Item::new(self);
self.values.push(item);
&self.values.last().unwrap()
}
fn with_first() -> Self {
let mut list = List::new();
list.push();
list
}
}
fn main() {
let mut list = List::new();
list.push();
assert_eq!(list.values[0].get_len(), 1);
let list = List::with_first();
assert_eq!(list.values[0].get_len(), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment