1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*!
A late-initialized static reference.
*/

use std::{
    sync::atomic::{AtomicPtr,Ordering},
    ptr,
};

use lock_api::RawMutex as RawMutexTrait;

use parking_lot::RawMutex;

/// A late-initialized static reference.
pub struct LazyStaticRef<T>{
    // Using a RawMutex because all I need is to prevent multiple threads 
    // from loading the static at the same time.
    lock:RawMutex,
    pointer:AtomicPtr<T>,
}

// Workaround for being unable to use traits inside `const fn`.
const LOCK:RawMutex=<RawMutex as RawMutexTrait>::INIT;


impl<T> LazyStaticRef<T>{
    /// Constructs the late initialized static reference,
    /// in an uninitialized state.
    pub const fn new()->Self{
        Self{
            lock:LOCK,
            pointer:AtomicPtr::new(ptr::null_mut()),
        }
    }

    /// Lazily initializes the reference with `initializer`,
    /// returning the reference if either it was already initialized,or
    /// if `initalizer` returned Ok(..).
    ///
    /// If `initializer` returns an `Err(...)` this returns the error and 
    /// allows the reference to be initializer later.
    ///
    /// If `initializer` panics,the panic is propagated,
    /// and the reference can be initalized later.
    pub fn try_init<F,E>(&self,initializer:F)->Result<&'static T,E>
    where F:FnOnce()->Result<&'static T,E>
    {
        if let Some(pointer)=self.get() {
            return Ok(pointer);
        }
        
        self.lock.lock();
        let unlocker=UnlockOnDrop(&self.lock);

        if let Some(pointer)=self.get() {
            return Ok(pointer);
        }

        let pointer=initializer()?;

        self.pointer.store(pointer as *const T as *mut T,Ordering::Release);

        drop(unlocker);

        Ok(pointer)

    }


    /// Lazily initializes the reference with `initializer`,
    /// returning the reference if either it was already initialized,or
    /// once `initalizer` returns the reference.
    ///
    /// If `initializer` panics,the panic is propagated,
    /// and the reference can be initalized later.
    #[inline]
    pub fn init<F>(&self,initializer:F)->&'static T
    where F:FnOnce()->&'static T
    {
        self
            .try_init(||->Result<&'static T,()>{ 
                Ok(initializer()) 
            })
            .expect("bug:LazyStaticRef::try_init should only return an Err if `initializer` does")
    }

    /// Returns `Some(x:&'static T)` if the reference was initialized,otherwise returns None.
    pub fn get(&self)->Option<&'static T>{
        unsafe{
            self.pointer
                .load(Ordering::Acquire)
                .as_ref()
        }
    }
}



struct UnlockOnDrop<'a>(&'a RawMutex);

impl<'a> Drop for UnlockOnDrop<'a>{
    fn drop(&mut self){
        (self.0).unlock();
    }
}


//////////////////////////////////////////////////////


#[cfg(test)]
mod tests{
    use super::*;

    use std::panic::catch_unwind;

    static N_100:u32=100;
    static N_277:u32=277;

    #[test]
    fn test_init(){
        let ptr=LazyStaticRef::<u32>::new();

        assert_eq!(None,ptr.get() );
        
        let caught=catch_unwind(||{
            ptr.init(|| panic!() );
        });
        assert!(caught.is_err());

        assert_eq!(None,ptr.get() );

        assert_eq!(100,*ptr.init(|| &N_100 ));
        assert_eq!(100,*ptr.init(|| panic!("this should not run") ));
        
        assert_eq!(
            (&N_100)as *const u32,
            ptr.get().unwrap() as *const u32 
        );

    }

    #[test]
    fn test_try_init(){
        let ptr=LazyStaticRef::<u32>::new();

        assert_eq!(None,ptr.get() );
        
        let caught=catch_unwind(||{
            let _=ptr.try_init(||->Result<_,i32>{ panic!() });
        });
        assert!(caught.is_err());

        assert_eq!(None,ptr.get() );

        assert_eq!(Err(10),ptr.try_init(||->Result<_,i32>{ Err(10) }));
        assert_eq!(Err(17),ptr.try_init(||->Result<_,i32>{ Err(17) }));

        assert_eq!(Ok(&277),ptr.try_init(||->Result<_,i32>{ Ok(&N_277) }));
        
        assert_eq!(
            Ok(&277),
            ptr.try_init(||->Result<_,i32>{ panic!("this should not run") })
        );
        
        assert_eq!(
            (&N_277)as *const u32,
            ptr.get().unwrap() as *const u32 
        );

    }
}