mod.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. use std::mem::align_of;
  2. use bytemuck::{Pod, Zeroable};
  3. pub mod account;
  4. pub mod mint;
  5. pub mod multisig;
  6. #[repr(C)]
  7. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  8. pub struct PodCOption<T: Default + PartialEq + Pod + Sized> {
  9. /// Indicates if the option is `Some` or `None`.
  10. tag: [u8; 4],
  11. /// The value of the option.
  12. value: T,
  13. }
  14. impl<T: Default + PartialEq + Pod + Sized> From<Option<T>> for PodCOption<T> {
  15. fn from(value: Option<T>) -> Self {
  16. if align_of::<T>() != 1 {
  17. panic!("PodCOption only supports Pod types with alignment 1");
  18. }
  19. match value {
  20. Some(value) => Self {
  21. tag: [1, 0, 0, 0],
  22. value,
  23. },
  24. None => Self {
  25. tag: [0, 0, 0, 0],
  26. value: T::default(),
  27. },
  28. }
  29. }
  30. }
  31. impl<T: Default + PartialEq + Pod + Sized> PodCOption<T> {
  32. pub const NONE: [u8; 4] = [0, 0, 0, 0];
  33. pub const SOME: [u8; 4] = [1, 0, 0, 0];
  34. pub fn some(value: T) -> Self {
  35. Self {
  36. tag: [1, 0, 0, 0],
  37. value,
  38. }
  39. }
  40. /// Returns `true` if the option is a `None` value.
  41. #[inline]
  42. pub fn is_none(&self) -> bool {
  43. self.tag == Self::NONE
  44. }
  45. /// Returns `true` if the option is a `Some` value.
  46. #[inline]
  47. pub fn is_some(&self) -> bool {
  48. !self.is_none()
  49. }
  50. /// Returns the contained value as an `Option`.
  51. #[inline]
  52. pub fn get(self) -> Option<T> {
  53. if self.is_none() {
  54. None
  55. } else {
  56. Some(self.value)
  57. }
  58. }
  59. /// Returns the contained value as an `Option`.
  60. #[inline]
  61. pub fn as_ref(&self) -> Option<&T> {
  62. if self.is_none() {
  63. None
  64. } else {
  65. Some(&self.value)
  66. }
  67. }
  68. /// Returns the contained value as a mutable `Option`.
  69. #[inline]
  70. pub fn as_mut(&mut self) -> Option<&mut T> {
  71. if self.is_none() {
  72. None
  73. } else {
  74. Some(&mut self.value)
  75. }
  76. }
  77. #[inline]
  78. pub fn set(&mut self, value: T) {
  79. self.tag = Self::SOME;
  80. self.value = value;
  81. }
  82. #[inline]
  83. pub fn clear(&mut self) {
  84. self.tag = Self::NONE;
  85. // we don't need to zero the value since the tag
  86. // indicates it is a `None` value
  87. }
  88. }
  89. /// ## Safety
  90. ///
  91. /// `PodCOption` requires a `Pod` type `T` with alignment of 1.
  92. unsafe impl<T: Default + PartialEq + Pod + Sized> Pod for PodCOption<T> {}
  93. /// ## Safety
  94. ///
  95. /// `PodCOption` requires a `Pod` type `T` with alignment of 1.
  96. unsafe impl<T: Default + PartialEq + Pod + Sized> Zeroable for PodCOption<T> {}
  97. #[repr(C)]
  98. #[derive(Copy, Clone, Default, Pod, Zeroable)]
  99. pub struct PodBool(u8);
  100. impl From<bool> for PodBool {
  101. fn from(b: bool) -> Self {
  102. Self(b.into())
  103. }
  104. }
  105. impl From<&bool> for PodBool {
  106. fn from(b: &bool) -> Self {
  107. Self((*b).into())
  108. }
  109. }
  110. impl From<&PodBool> for bool {
  111. fn from(b: &PodBool) -> Self {
  112. b.0 != 0
  113. }
  114. }
  115. impl From<PodBool> for bool {
  116. fn from(b: PodBool) -> Self {
  117. b.0 != 0
  118. }
  119. }
  120. #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
  121. #[repr(C)]
  122. pub struct PodU64(pub [u8; 8]);
  123. impl PodU64 {
  124. pub const fn from_primitive(n: u64) -> Self {
  125. Self(n.to_le_bytes())
  126. }
  127. }
  128. impl From<u64> for PodU64 {
  129. fn from(n: u64) -> Self {
  130. Self::from_primitive(n)
  131. }
  132. }
  133. impl From<PodU64> for u64 {
  134. fn from(pod: PodU64) -> Self {
  135. Self::from_le_bytes(pod.0)
  136. }
  137. }