From b22254cb323d62cd270deaf1bf6cd15ba94d002c Mon Sep 17 00:00:00 2001 From: Janis Date: Sun, 6 Jul 2025 15:04:58 +0200 Subject: [PATCH] comments --- src/sync.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sync.rs b/src/sync.rs index 4bb9184..94a91c1 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -357,7 +357,8 @@ pub mod channel { } /// Takes the value from the channel, if it is present. - fn take(&mut self) -> Option { + /// this function must only ever return `Some` once. + unsafe fn take(&mut self) -> Option { // unset the OCCUPIED_BIT to indicate that we are taking the value, if any is present. if self .0 @@ -369,13 +370,19 @@ pub mod channel { // The channel was empty, so we return None. None } else { + // SAFETY: we only ever access this field by pointer + // the OCCUPIED_BIT was set, so we can safely read the value. + // this function is only called once, within `recv`, + // guaranteeing that the value will only be dropped once. unsafe { Some(self.0.val.get().read().assume_init_read()) } } } pub fn recv(mut self) -> T { loop { - if let Some(t) = self.take() { + // SAFETY: recv can only be called once, since it takes ownership of `self`. + // if `take` returns a value, it will never be called again. + if let Some(t) = unsafe { self.take() } { return t; }