-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod pod_bool; | ||
mod pod_option; | ||
mod pod_str; | ||
|
||
pub use pod_bool::*; | ||
pub use pod_option::*; | ||
pub use pod_str::*; |
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,41 @@ | ||
use bytemuck::{Pod, Zeroable}; | ||
|
||
/// Used for "pod-enabled" types that can have a `None` value. | ||
pub trait Nullable: Pod { | ||
/// Indicates if the value is `Some`. | ||
fn is_some(&self) -> bool; | ||
|
||
/// Indicates if the value is `None`. | ||
fn is_none(&self) -> bool; | ||
} | ||
|
||
/// A "pod-enabled" type that can be used as an `Option<T>` without | ||
/// requiring extra space to indicate if the value is `Some` or `None`. | ||
/// | ||
/// This can be used when a specific value of `T` indicates that its | ||
/// value is `None`. | ||
#[repr(C)] | ||
#[derive(Clone, Copy)] | ||
pub struct PodOption<T: Nullable>(T); | ||
|
||
unsafe impl<T: Nullable> Pod for PodOption<T> {} | ||
|
||
unsafe impl<T: Nullable> Zeroable for PodOption<T> {} | ||
|
||
impl<T: Nullable> PodOption<T> { | ||
pub fn value(&self) -> Option<&T> { | ||
if self.0.is_some() { | ||
Some(&self.0) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn value_mut(&mut self) -> Option<&mut T> { | ||
if self.0.is_some() { | ||
Some(&mut self.0) | ||
} else { | ||
None | ||
} | ||
} | ||
} |