intrusive-rs icon indicating copy to clipboard operation
intrusive-rs copied to clipboard

Great crate, major hole in it ;-)

Open przygienda opened this issue 6 years ago • 10 comments

Crate is great. Very common problem though in this kind of stuff. I insert into rbtree, then use the link list to change keys, tree ends up corrupted since it seems to pull local copies of keys (basically invariants change). Adapters would need to somehow let each other know when the key is being modified or some other kind of API fix to prevent this.

// get bunch elements into tree/lists & then flip keys & see whether sorting on the
// tree holds

	let mut a = LinkedList::new(MyAdapter::new());
	let mut b = SinglyLinkedList::new(MyAdapter2::new());
	let mut c = RBTree::new(MyAdapter3::new());

	for v in &[30, 40, 50, 60, 70, 80, 90, 100] {
		let mut test = Rc::new(Test {
			value: Cell::new(*v),
			..Test::default()
		});
		a.push_front(test.clone());
		b.push_front(test.clone());
		c.insert(test);
	}

	{
// Find the first element which is greater than or equal to min
		let mut cursor = c.lower_bound_mut(Bound::Included(&40));

		let mut cont = true;
		// Iterate over all elements in the range [min, max]
		while !cursor.is_null() {
			let v = cursor.get();
			println!("{:?}", v);

			cursor.move_next();
		}
	}

	let mut v = 0xff; // pseudo rand

	let deft = Test {
		value: Cell::new(99),
		..Test::default()
	};

	// divide by ten
	let mut mc = a.cursor_mut();

	mc.move_next();

	while ! mc.is_null() {
		let iv = &mc.get().unwrap_or(&deft).value;

		if let Some(imc) = mc.get() {
			println!("{} -> {}", iv.get(), iv.get() + v);
			imc.value.set(iv.get() + v);
		}

		v ^= iv.get();
		mc.move_next();
	}

	{
		let mut cursor = c.lower_bound_mut(Bound::Included(&40));

		let mut cont = true;
		// Iterate over all elements in the range [min, max]
		while !cursor.is_null() {
			let v = cursor.get();
			println!("{:?}", v);

			cursor.move_next();
		}
	}

przygienda avatar Apr 19 '18 07:04 przygienda