swapper

Swap ownership of data between threads in Rust

MPL-2.0 License

Downloads
206.3K
Stars
2
Committers
2

swapper

Swap ownership of data between threads in Rust.

This crate allows threads to swap ownership of data without a transitory state where the thread owns nothing.

extern crate swapper;
pub fn main() {
   let (ab, ba) = swapper::swapper();
   std::thread::spawn(move || {
      let mut a = String::from("hello");
      ab.swap(&mut a).unwrap();
      assert_eq!(a, "world");
   });
   let mut b = String::from("world");
   ba.swap(&mut b).unwrap();
   assert_eq!(b, "hello");
}