From f49382c0508db608aa9c33d6a8c77d2955d8f62c Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Thu, 31 Aug 2023 22:12:47 +0200 Subject: Use `Freeze` for `SourceFile.lines` --- compiler/rustc_data_structures/src/sync/freeze.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'compiler/rustc_data_structures/src/sync') diff --git a/compiler/rustc_data_structures/src/sync/freeze.rs b/compiler/rustc_data_structures/src/sync/freeze.rs index 88c710e5236..ad71ae1af51 100644 --- a/compiler/rustc_data_structures/src/sync/freeze.rs +++ b/compiler/rustc_data_structures/src/sync/freeze.rs @@ -3,6 +3,7 @@ use crate::sync::{AtomicBool, ReadGuard, RwLock, WriteGuard}; use crate::sync::{DynSend, DynSync}; use std::{ cell::UnsafeCell, + intrinsics::likely, marker::PhantomData, ops::{Deref, DerefMut}, sync::atomic::Ordering, @@ -49,6 +50,17 @@ impl FreezeLock { self.frozen.load(Ordering::Acquire) } + /// Get the inner value if frozen. + #[inline] + pub fn get(&self) -> Option<&T> { + if likely(self.frozen.load(Ordering::Acquire)) { + // SAFETY: This is frozen so the data cannot be modified. + unsafe { Some(&*self.data.get()) } + } else { + None + } + } + #[inline] pub fn read(&self) -> FreezeReadGuard<'_, T> { FreezeReadGuard { -- cgit 1.4.1-3-g733a5