-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/fix-self-referential-ub
- Loading branch information
Showing
6 changed files
with
157 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use core::mem::MaybeUninit; | ||
|
||
/// Simple wrapper around [`MaybeUninit`] that guarantees the type is initialized. | ||
/// | ||
/// This may sound odd, but the compiler can not assume that `MaybeUninit` is initialized, | ||
/// but as far is the compiler is concerned, `MaybeUninit<T>` is not keeping any references around, even if `T` would. | ||
/// So `NonAliasing<T>` is not aliassing anything untill you call `inner()` to get your `&T` back. | ||
/// | ||
/// We use this to create a (hopefully) sound self referential struct in `TemplateBuf` and `ByteTemplateBuf`. | ||
pub struct NonAliasing<T> { | ||
inner: MaybeUninit<T>, | ||
} | ||
|
||
impl<T> NonAliasing<T> { | ||
pub fn new(inner: T) -> Self { | ||
let inner = MaybeUninit::new(inner); | ||
Self { inner } | ||
} | ||
|
||
pub fn inner(&self) -> &T { | ||
// SAFETY: We always initialize `inner` in the constructor. | ||
unsafe { | ||
self.inner.assume_init_ref() | ||
} | ||
} | ||
} | ||
|
||
impl<T> Drop for NonAliasing<T> { | ||
fn drop(&mut self) { | ||
// SAFETY: We always initialize `inner` in the constructor, | ||
// the API only exposes `assume_init_ref()`, | ||
// and we're in the destructor, so nobody is going to call assume_init_read() again. | ||
unsafe { | ||
drop(self.inner.assume_init_read()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters