nlib 0.1.3

Nate's library. Various things or macro patterns I use to aid in more succint Rust programming.
Documentation
// Copyright 2025 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.

use std::{mem::MaybeUninit, sync::Once};

use crate::SyncUnsafeCell;

pub struct OnceInit<T> {
    inner: SyncUnsafeCell<MaybeUninit<T>>,
    init: Once,
}

impl<T> OnceInit<T> {
    pub const fn uninit() -> Self {
        Self {
            inner: SyncUnsafeCell::new(MaybeUninit::uninit()),
            init: Once::new(),
        }
    }

    #[track_caller]
    pub fn get(&self) -> &T {
        assert!(self.init.is_completed());

        let ptr = self.inner.get();
        unsafe { (*ptr).assume_init_ref() }
    }

    pub fn set(&self, val: T) {
        self.init.call_once(|| {
            let ptr = self.inner.get();
            unsafe { (*ptr).write(val) };
        });
    }
}