[][src]Struct abi_stable::external_types::parking_lot::once::ROnce

#[repr(C)]pub struct ROnce { /* fields omitted */ }

A synchronization primitive for running global initialization once.

Example

use abi_stable::external_types::{ROnce,RMutex};

static MUTEX:RMutex<usize>=RMutex::new(0);

static ONCE:ROnce=ROnce::new();

let guards=
    std::iter::repeat_with(||{
        std::thread::spawn(||{
            ONCE.call_once(||{
                *MUTEX.lock()+=1;
            })
        })
    })
    .take(20)
    .collect::<Vec<_>>();

for guard in guards{
    guard.join().unwrap();
}

assert_eq!(*MUTEX.lock(),1);

Implementations

impl ROnce[src]

pub const fn new() -> ROnce[src]

Constructs an ROnce.

Example

use abi_stable::external_types::ROnce;

static ONCE: ROnce = ROnce::new();
 

pub const NEW: Self[src]

Constructs an ROnce.

Example

use abi_stable::external_types::ROnce;

static ONCE:ROnce=ROnce::NEW;

pub fn state(&self) -> ROnceState[src]

Gets the running state of this ROnce.

Example

use abi_stable::external_types::parking_lot::once::{ROnce,ROnceState};

use std::panic::AssertUnwindSafe;

let once=ROnce::new();

assert_eq!(once.state(), ROnceState::New );

let _=std::panic::catch_unwind(AssertUnwindSafe(||{
    once.call_once(|| panic!() );
}));

assert!( once.state().poisoned() );

once.call_once_force(|_| () );

assert!( once.state().done() );

pub fn call_once<F>(&self, f: F) where
    F: FnOnce(), 
[src]

Runs an initialization function.

f will be run only if this is the first time this method has been called on this ROnce.

Once this function returns it is guaranteed that some closure passed to this method has run to completion.

Panics

Panics in the closure will cause this ROnce to become poisoned, and any future calls to this method will panic.

Example

use abi_stable::external_types::ROnce;

let once=ROnce::new();
let mut counter=0usize;

once.call_once(|| counter+=1 );
once.call_once(|| counter+=1 );
once.call_once(|| counter+=1 );
once.call_once(|| counter+=1 );

assert_eq!(counter,1);

pub fn call_once_force<F>(&self, f: F) where
    F: FnOnce(ROnceState), 
[src]

Runs an initialization function,even if the ROnce is poisoned.

This will keep trying to run different closures until one of them doesn't panic.

The ROnceState parameter describes whether the ROnce is New or Poisoned.

Example

use abi_stable::external_types::ROnce;

use std::panic::{self,AssertUnwindSafe};

let once=ROnce::new();
let mut counter=0usize;

for i in 0..100 {
    let _=panic::catch_unwind(AssertUnwindSafe(||{
        once.call_once_force(|_|{
            if i < 6 {
                panic!();
            }
            counter=i;
        })
    }));
}

assert_eq!(counter,6);

Trait Implementations

impl Debug for ROnce[src]

impl Default for ROnce[src]

impl GetStaticEquivalent_ for ROnce[src]

type StaticEquivalent = _static_ROnce

impl Send for ROnce[src]

impl StableAbi for ROnce[src]

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more

impl Sync for ROnce[src]

Auto Trait Implementations

impl RefUnwindSafe for ROnce

impl Unpin for ROnce

impl UnwindSafe for ROnce

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more