SHARE
TWEET

Untitled

a guest Jul 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::{
  2.     mem,
  3.     sync::{
  4.         atomic::{AtomicPtr, Ordering},
  5.         Arc,
  6.     },
  7. };
  8.  
  9. struct A(AtomicPtr<String>);
  10.  
  11. impl A {
  12.     fn replace(&self, s: String) -> Arc<String> {
  13.         let s = Arc::new(s);
  14.         let s = Arc::into_raw(s) as *mut String;
  15.  
  16.         let prev = self.0.swap(s, Ordering::SeqCst);
  17.  
  18.         unsafe { Arc::from_raw(prev) }
  19.     }
  20.  
  21.     fn get(&self) -> Arc<String> {
  22.         let current = self.0.load(Ordering::SeqCst);
  23.         let current = unsafe { Arc::from_raw(current) };
  24.  
  25.         // Increment the counter because we aren't taking ownership of the value in self
  26.         mem::forget(current.clone());
  27.  
  28.         current
  29.     }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top