DataModel

Enum DataModel 

Source
pub enum DataModel {
    IP16,
    IP16L32,
    LP32,
    ILP32,
    LLP64,
    LP64,
    ILP64,
    SILP64,
    Unknown,
}
Expand description

A data model is the choices of bit width of integer types by each platform.

§Examples

use data_models::*;
let model = DataModel::LP64; // e.g. Linux
let p = model.size_of::<Pointer>();
assert_eq!(p, 8);

§Background

The C standard defines five base types for integers

  • char
  • short
  • int
  • long
  • long long

The standard does not specify the exact number of bits for each type. A platform or vendor-dependent data model specifies the exact bit widths.

The names of the models are conventions where the type is signified by a letter and its size; for example, ILP32 would mean (I)nteger, (L)ong, and (P)ointer are 32-bits. Although, make note, the naming scheme is not super consistent.

Four data models found wide acceptance:

  • LP32 or 2/4/4 (int is 16-bit, long and pointer are 32-bit) M68k mac and Win16 API

  • ILP32 or 4/4/4 (int, long, and pointer are 32-bit); Win32 API Unix and Unix-like systems (Linux, Mac OS X)

  • LLP64 or 4/4/8 (int and long are 32-bit, pointer is 64-bit) Win64 API

  • LP64 or 4/8/8 (int is 32-bit, long and pointer are 64-bit) Unix and Unix-like systems (Linux, Mac OS X)

§References

  1. J. R. Mashey. The long road to 64 bits. ACM Queue Magazine, 4(8):24–35, 1996.
  2. T. Lauer. Porting to Win32: A Guide to Making Your Applications Ready for the 32-Bit Future of Windows. Springer, 1996.

Variants§

§

IP16

16-bit integer and pointer (16-bit PDP-11)

§

IP16L32

16-bit integer and pointer and 32-bit long (32-bit PDP-11)

§

LP32

16-bit integer, and 32-bit long and pointer (m68k Mac & win16).

§

ILP32

32-bit integer, long, and pointer (Unix and Unix-like before mid-1990s & win32).

§

LLP64

32-bit integer, long, and 64-bit pointer (windows after XP).

§

LP64

32-bit integer, 64-bit long and pointer (Unix/Linux after the 1990s).

§

ILP64

64-bit integer, long and pointer (SPARC64 from hal/fujitsu I think).

§

SILP64

64-bit short, integer, long and pointer (UNICOS from Cray).

§

Unknown

Sentinel value for unknown model.

Implementations§

Source§

impl DataModel

Source

pub fn new(int: usize, long: usize, pointer: usize) -> DataModel

new tries to guess the data model from the byte size of int, long, and pointer.

§Example
use data_models::*;
let model = DataModel::new(4, 8, 8); // LP64
let p = model.size_of::<Pointer>();
assert_eq!(p, 8);
Source

pub fn size_of<T>(self) -> usize

size_of will report the size in bytes for one of the types defined in this crate.

§Example
use data_models::*;
let model = DataModel::LLP64;
let p = model.size_of::<Long>();
assert_eq!(p, 4);

Trait Implementations§

Source§

impl Debug for DataModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DataModel

Source§

fn eq(&self, other: &DataModel) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DataModel

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.