Guest User

Untitled

a guest
Jun 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. use std::sync::{Once, ONCE_INIT};
  2.  
  3. pub struct Lazy<T: Sync>(pub Option<T>, pub Once);
  4.  
  5. impl<T: Sync> Lazy<T> {
  6. #[inline(always)]
  7. pub fn get<F>(&'static mut self, f: F) -> &T
  8. where F: FnOnce() -> T
  9. {
  10. {
  11. let r = &mut self.0;
  12. self.1.call_once(|| {
  13. *r = Some(f());
  14. });
  15. }
  16. unsafe {
  17. match self.0 {
  18. Some(ref x) => x,
  19. None => std::hint::unreachable_unchecked(),
  20. }
  21. }
  22. }
  23. }
  24.  
  25. unsafe impl<T: Sync> Sync for Lazy<T> {}
  26.  
  27. #[macro_export]
  28. #[doc(hidden)]
  29. macro_rules! __lazy_static_create {
  30. ($NAME:ident, $T:ty) => {
  31. static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(None, $crate::lazy::ONCE_INIT);
  32. }
  33. }
  34.  
  35. fn main() {}
Add Comment
Please, Sign In to add comment