fp_library/types/
once_lock.rs

1//! Implementations for [`OnceLock`][std::sync::OnceLock]
2
3use crate::{
4	classes::{Once, once::ApplyOnce},
5	hkt::{Apply0L1T, Kind0L1T},
6};
7use std::sync::OnceLock;
8
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct OnceLockBrand;
11
12impl Kind0L1T for OnceLockBrand {
13	type Output<A> = OnceLock<A>;
14}
15
16impl Once for OnceLockBrand {
17	type Output<A> = Apply0L1T<Self, A>;
18
19	fn new<A>() -> ApplyOnce<Self, A> {
20		OnceLock::new()
21	}
22
23	fn get<A>(a: &ApplyOnce<Self, A>) -> Option<&A> {
24		OnceLock::get(a)
25	}
26
27	fn get_mut<A>(a: &mut ApplyOnce<Self, A>) -> Option<&mut A> {
28		OnceLock::get_mut(a)
29	}
30
31	fn set<A>(
32		a: &ApplyOnce<Self, A>,
33		value: A,
34	) -> Result<(), A> {
35		OnceLock::set(a, value)
36	}
37
38	fn get_or_init<A, B: FnOnce() -> A>(
39		a: &ApplyOnce<Self, A>,
40		f: B,
41	) -> &A {
42		OnceLock::get_or_init(a, f)
43	}
44
45	fn into_inner<A>(a: ApplyOnce<Self, A>) -> Option<A> {
46		OnceLock::into_inner(a)
47	}
48
49	fn take<A>(a: &mut ApplyOnce<Self, A>) -> Option<A> {
50		OnceLock::take(a)
51	}
52}