This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[libraries/pod]: add PodOption
type
#6886
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4a3e842
Add pod option type
febo 93ec3d0
Use nightly fmt
febo 0d81c61
Rename to option
febo 1196e6f
Update description
febo 00bddad
Remove interger types impl
febo 500da51
Use From impl
febo 2de357e
Add get method
febo 68b9cf8
Cleanup API
febo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,138 @@ | ||
//! Generic `Option` that can be used as a `Pod` for types that can have | ||
//! a designated `None` value. | ||
//! | ||
//! For example, a 64-bit unsigned integer can designate `0` as a `None` value. | ||
//! This would be equivalent to | ||
//! [`Option<NonZeroU64>`](https://doc.rust-lang.org/std/num/type.NonZeroU64.html) | ||
//! and provide the same memory layout optimization. | ||
|
||
use { | ||
bytemuck::{Pod, Zeroable}, | ||
solana_program::{program_option::COption, pubkey::Pubkey}, | ||
}; | ||
|
||
/// Trait for types that can be `None`. | ||
/// | ||
/// This trait is used to indicate that a type can be `None` according to a | ||
/// specific value. | ||
pub trait Nullable: Default + Pod { | ||
/// Indicates whether the value is `None` or not. | ||
fn is_none(&self) -> bool; | ||
|
||
/// Indicates whether the value is `Some`` value of type `T`` or not. | ||
fn is_some(&self) -> bool { | ||
!self.is_none() | ||
} | ||
} | ||
|
||
/// 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(transparent)] | ||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] | ||
pub struct PodOption<T: Nullable>(T); | ||
|
||
impl<T: Nullable> PodOption<T> { | ||
/// Returns the contained value as an `Option`. | ||
#[inline] | ||
pub fn get(self) -> Option<T> { | ||
if self.0.is_none() { | ||
None | ||
} else { | ||
Some(self.0) | ||
} | ||
} | ||
|
||
/// Returns the contained value as an `Option`. | ||
#[inline] | ||
pub fn as_ref(&self) -> Option<&T> { | ||
if self.0.is_none() { | ||
None | ||
} else { | ||
Some(&self.0) | ||
} | ||
} | ||
|
||
/// Returns the contained value as a mutable `Option`. | ||
#[inline] | ||
pub fn as_mut(&mut self) -> Option<&mut T> { | ||
if self.0.is_none() { | ||
None | ||
} else { | ||
Some(&mut self.0) | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<T: Nullable> Pod for PodOption<T> {} | ||
|
||
unsafe impl<T: Nullable> Zeroable for PodOption<T> {} | ||
febo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl<T: Nullable> From<T> for PodOption<T> { | ||
fn from(value: T) -> Self { | ||
PodOption(value) | ||
} | ||
} | ||
|
||
impl<T: Nullable> From<Option<T>> for PodOption<T> { | ||
fn from(from: Option<T>) -> Self { | ||
match from { | ||
Some(value) => PodOption(value), | ||
None => PodOption(T::default()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only caveat is that in this case
|
||
} | ||
} | ||
} | ||
febo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl<T: Nullable> From<COption<T>> for PodOption<T> { | ||
fn from(from: COption<T>) -> Self { | ||
match from { | ||
COption::Some(value) => PodOption(value), | ||
COption::None => PodOption(T::default()), | ||
} | ||
} | ||
} | ||
febo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Implementation of `Nullable` for `Pubkey`. | ||
/// | ||
/// The implementation assumes that the default value of `Pubkey` represents | ||
/// the `None` value. | ||
impl Nullable for Pubkey { | ||
fn is_none(&self) -> bool { | ||
self == &Pubkey::default() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use {super::*, crate::bytemuck::pod_slice_from_bytes, solana_program::sysvar}; | ||
|
||
#[test] | ||
fn test_pod_option_pubkey() { | ||
let some_pubkey = PodOption::from(sysvar::ID); | ||
assert_eq!(some_pubkey.get(), Some(sysvar::ID)); | ||
|
||
let none_pubkey = PodOption::from(Pubkey::default()); | ||
assert_eq!(none_pubkey.get(), None); | ||
|
||
let mut data = Vec::with_capacity(64); | ||
data.extend_from_slice(sysvar::ID.as_ref()); | ||
data.extend_from_slice(&[0u8; 32]); | ||
|
||
let values = pod_slice_from_bytes::<PodOption<Pubkey>>(&data).unwrap(); | ||
assert_eq!(values[0], PodOption::from(sysvar::ID)); | ||
assert_eq!(values[1], PodOption::from(Pubkey::default())); | ||
|
||
let option_pubkey = Some(sysvar::ID); | ||
let pod_option_pubkey: PodOption<Pubkey> = option_pubkey.into(); | ||
assert_eq!(pod_option_pubkey, PodOption::from(sysvar::ID)); | ||
assert_eq!(pod_option_pubkey, PodOption::from(option_pubkey)); | ||
|
||
let coption_pubkey = COption::Some(sysvar::ID); | ||
let pod_option_pubkey: PodOption<Pubkey> = coption_pubkey.into(); | ||
assert_eq!(pod_option_pubkey, PodOption::from(sysvar::ID)); | ||
assert_eq!(pod_option_pubkey, PodOption::from(coption_pubkey)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.