From a8034c79aa5cecb08953a9733e385b30e157eb16 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 5 Feb 2024 13:59:26 -0800 Subject: [PATCH] [bitboard] Use a OnceLock to hold the global BitBoard library instance Thread safe and a single object instead of two! --- bitboard/src/library.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bitboard/src/library.rs b/bitboard/src/library.rs index 4615caa..afc4240 100644 --- a/bitboard/src/library.rs +++ b/bitboard/src/library.rs @@ -2,7 +2,7 @@ use crate::BitBoard; use chessfriend_core::{Color, Direction, Square}; -use std::sync::Once; +use std::sync::OnceLock; pub(super) const RANKS: [BitBoard; 8] = [ BitBoard(0xFF << 0 * 8), @@ -42,15 +42,15 @@ pub(crate) const LIGHT_SQUARES: BitBoard = pub(crate) const DARK_SQUARES: BitBoard = BitBoard(!LIGHT_SQUARES.0); pub(super) fn library() -> &'static MoveLibrary { - static MOVE_LIBRARY_INIT: Once = Once::new(); - static mut MOVE_LIBRARY: MoveLibrary = MoveLibrary::new(); + static mut MOVE_LIBRARY: OnceLock = OnceLock::new(); unsafe { - MOVE_LIBRARY_INIT.call_once(|| { - MOVE_LIBRARY.init(); - }); + MOVE_LIBRARY.get_or_init(|| { + let mut library = MoveLibrary::new(); + library.init(); - &MOVE_LIBRARY + library + }) } }