This commit is contained in:
nora 2023-01-15 11:32:11 +01:00
parent afb32dbd6a
commit 2560ba4253
3 changed files with 41 additions and 4 deletions

View 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| {});
}
}
}

View file

@ -4,6 +4,7 @@
#![feature(negative_impls)]
pub mod cfg_match;
pub mod innocent_linked_list;
pub mod sendsync;
pub mod unroll_int;
pub mod unsized_clone;

View file

@ -1,3 +1,5 @@
#![cfg_attr(not(test), allow(unused))]
use std::{
cell::{Cell, UnsafeCell},
rc::Rc,
@ -27,9 +29,9 @@ fn rc_the_new_contender() {
let x = Rc::new(0);
let x2 = x.clone();
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.
@ -53,9 +55,9 @@ fn but_arc_is_fine() {
let x = Arc::new(0);
let x2 = x.clone();
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)