| 1 |
//! Pointer width of the target architecture |
| 2 |
|
| 3 |
use crate::error::Error; |
| 4 |
use core::{fmt, str::FromStr}; |
| 5 |
|
| 6 |
#[cfg(feature = "serde")] |
| 7 |
use serde::{de, de::Error as DeError, ser, Deserialize, Serialize}; |
| 8 |
|
| 9 |
/// `target_pointer_width`: Size of native pointer types (`usize`, `isize`) in bits |
| 10 |
/// |
| 11 |
/// 64 bits for modern desktops and phones, 32-bits for older devices, 16 bits for certain microcontrollers |
| 12 |
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] |
| 13 |
#[non_exhaustive] |
| 14 |
pub enum PointerWidth { |
| 15 |
/// `16` |
| 16 |
U16, |
| 17 |
|
| 18 |
/// `32` |
| 19 |
U32, |
| 20 |
|
| 21 |
/// `64` |
| 22 |
U64, |
| 23 |
} |
| 24 |
|
| 25 |
impl PointerWidth { |
| 26 |
/// String representing this `PointerWidth` which matches `#[cfg(target_pointer_width)]` |
| 27 |
pub fn as_str(self) -> &'static str { |
| 28 |
match self { |
| 29 |
PointerWidth::U16 => "16", |
| 30 |
PointerWidth::U32 => "32", |
| 31 |
PointerWidth::U64 => "64", |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl FromStr for PointerWidth { |
| 37 |
type Err = Error; |
| 38 |
|
| 39 |
/// Create a new `PointerWidth` from the given string |
| 40 |
fn from_str(name: &str) -> Result<Self, Self::Err> { |
| 41 |
let result = match name { |
| 42 |
"16" => PointerWidth::U16, |
| 43 |
"32" => PointerWidth::U32, |
| 44 |
"64" => PointerWidth::U64, |
| 45 |
_ => return Err(Error), |
| 46 |
}; |
| 47 |
|
| 48 |
Ok(result) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
use core::convert::TryFrom; |
| 53 |
|
| 54 |
impl TryFrom<u8> for PointerWidth { |
| 55 |
type Error = &'static str; |
| 56 |
|
| 57 |
fn try_from(value: u8) -> Result<Self, Self::Error> { |
| 58 |
match value { |
| 59 |
64 => Ok(PointerWidth::U64), |
| 60 |
32 => Ok(PointerWidth::U32), |
| 61 |
16 => Ok(PointerWidth::U16), |
| 62 |
_ => Err("Invalid pointer width!"), |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl From<PointerWidth> for u8 { |
| 68 |
fn from(value: PointerWidth) -> Self { |
| 69 |
match value { |
| 70 |
PointerWidth::U64 => 64, |
| 71 |
PointerWidth::U32 => 32, |
| 72 |
PointerWidth::U16 => 16, |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl fmt::Display for PointerWidth { |
| 78 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 79 |
f.write_str(self.as_str()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(feature = "serde")] |
| 84 |
impl Serialize for PointerWidth { |
| 85 |
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 86 |
serializer.serialize_str(self.as_str()) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[cfg(all(feature = "serde", feature = "std"))] |
| 91 |
impl<'de> Deserialize<'de> for PointerWidth { |
| 92 |
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 93 |
let string = std::string::String::deserialize(deserializer)?; |
| 94 |
string.parse().map_err(|_| { |
| 95 |
D::Error::custom(std::format!( |
| 96 |
"Unrecognized value '{}' for target_pointer_width", |
| 97 |
string |
| 98 |
)) |
| 99 |
}) |
| 100 |
} |
| 101 |
} |