mirror of
https://github.com/Noratrieb/the-good-stuff.git
synced 2026-01-17 10:05:00 +01:00
linked
This commit is contained in:
parent
afb32dbd6a
commit
2560ba4253
3 changed files with 41 additions and 4 deletions
34
src/innocent_linked_list.rs
Normal file
34
src/innocent_linked_list.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
pub struct Node<'a, 'n, T> {
|
||||||
|
item: T,
|
||||||
|
outer: Option<&'a mut Node<'a, 'n, T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'n, T> Node<'a, 'n, T> {
|
||||||
|
pub fn new(item: T) -> Self {
|
||||||
|
Self { item, outer: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push<R>(&mut self, item: T, with_func: impl FnOnce(&mut Self) -> R) -> R {
|
||||||
|
let mut inner = Node {
|
||||||
|
item,
|
||||||
|
outer: Some(self),
|
||||||
|
};
|
||||||
|
with_func(&mut inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::Node;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn push() {
|
||||||
|
let mut list = Node::<u8>::new(0);
|
||||||
|
|
||||||
|
inner(&mut list);
|
||||||
|
|
||||||
|
fn inner(list: &mut Node<u8>) {
|
||||||
|
list.push(1, |list| {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#![feature(negative_impls)]
|
#![feature(negative_impls)]
|
||||||
|
|
||||||
pub mod cfg_match;
|
pub mod cfg_match;
|
||||||
|
pub mod innocent_linked_list;
|
||||||
pub mod sendsync;
|
pub mod sendsync;
|
||||||
pub mod unroll_int;
|
pub mod unroll_int;
|
||||||
pub mod unsized_clone;
|
pub mod unsized_clone;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![cfg_attr(not(test), allow(unused))]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, UnsafeCell},
|
cell::{Cell, UnsafeCell},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
|
@ -27,9 +29,9 @@ fn rc_the_new_contender() {
|
||||||
let x = Rc::new(0);
|
let x = Rc::new(0);
|
||||||
let x2 = x.clone();
|
let x2 = x.clone();
|
||||||
spawn(move || {
|
spawn(move || {
|
||||||
x2.clone();
|
let _ = x2.clone();
|
||||||
});
|
});
|
||||||
x.clone(); // DATA RACE
|
let _ = x.clone(); // DATA RACE
|
||||||
}
|
}
|
||||||
|
|
||||||
// Oh no, we have a data race. This is not exactly good, in fact it's really bad.
|
// Oh no, we have a data race. This is not exactly good, in fact it's really bad.
|
||||||
|
|
@ -53,9 +55,9 @@ fn but_arc_is_fine() {
|
||||||
let x = Arc::new(0);
|
let x = Arc::new(0);
|
||||||
let x2 = x.clone();
|
let x2 = x.clone();
|
||||||
spawn(move || {
|
spawn(move || {
|
||||||
x2.clone();
|
let _ = x2.clone();
|
||||||
});
|
});
|
||||||
x.clone();
|
let _ = x.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arc is fine here because it uses atomics internally. But it fails to compile! Here, Arc (or us in this case)
|
// Arc is fine here because it uses atomics internally. But it fails to compile! Here, Arc (or us in this case)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue