Struct PtrBool

Source
pub struct PtrBool<T> { /* private fields */ }
Expand description

A pointer and boolean bitpacked together.

PtrBools are the same size as a raw pointer; four bytes on a 32-bit system and eight bytes on a 64-bit system. This is done by storing in the boolean value in the one-bit of the pointer.

§Caveats

  • The pointer must be aligned by two to ensure that the one-bit is unnecessary and can be used to stored the boolean value.

  • Possibly a slight overhead when reading the values of the PtrBool, as the boolean value must be omitted from the pointer each time it’s read, and the pointer value must be omitted from the boolean value each time it is read. That is, unless there is some sort of unknown optimization built into Rust for the case of storing a boolean in one bit.

  • The value stored inside of a PtrBool must be Sized. This is because the pointer is converted to and from a usize; which loses any metadata which would have been in the raw pointer.

Implementations§

Source§

impl<T> PtrBool<T>

Source

pub fn new(ptr: *mut T, bool_: bool) -> Option<Self>

Initializes a PtrBool from the provided pointer and boolean values if the pointer is aligned by two.

§Examples
use ptr_bool::PtrBool;

let mut my_value = 42;
let ptr = PtrBool::new(&mut my_value as *mut _, true).unwrap();

assert_eq!(ptr.as_bool(), true);
unsafe {
    assert_eq!(*ptr.as_ref().unwrap(), 42);
}
Source

pub fn null(bool_: bool) -> Option<Self>

Initializes a PtrBool with a null pointer and the provided boolean value. Returns None if the type of this PtrBool is not aligned by 2.

§Examples
use ptr_bool::PtrBool;

let ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.as_bool(), true);
assert_eq!(ptr.is_null(), true);
Source

pub fn as_bool(&self) -> bool

Converts this PtrBool into a boolean value.

§Examples
use ptr_bool::PtrBool;

let ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.as_bool(), true);
Source

pub fn set_bool(&mut self, bool_: bool)

Sets the inner boolean value of this pointer.

§Examples
use ptr_bool::PtrBool;

let mut ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.as_bool(), true);
ptr.set_bool(false);
assert_eq!(ptr.as_bool(), false);
Source

pub fn as_ptr(&self) -> *const T

Converts this PtrBool into a raw pointer.

§Examples
use ptr_bool::PtrBool;
use std::ptr;

let mut ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.as_ptr(), ptr::null());
Source

pub fn as_mut_ptr(&self) -> *mut T

Converts this PtrBool into a raw mutable pointer.

§Examples
use ptr_bool::PtrBool;
use std::ptr;

let mut ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.as_mut_ptr(), ptr::null_mut());
Source

pub fn is_null(&self) -> bool

Returns true if the internal pointer is null.

§Examples
use ptr_bool::PtrBool;
use std::ptr;

let mut ptr: PtrBool<usize> = PtrBool::null(true).unwrap();

assert_eq!(ptr.is_null(), true);
Source

pub unsafe fn as_ref<'a>(&self) -> Option<&'a T>

Converts this PtrBool into a reference.

§Safety

By calling this method you promise that:

  • The wrapped pointer must be properly aligned.

  • This pointer must be dereferenceable in the sense defined in the ptr documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get mutated (except inside UnsafeCell).

If you are completely sure this pointer is not null; then the as_ref_unchecked method can be used instead.

§Examples
use ptr_bool::PtrBool;

let raw_ptr = Box::into_raw(Box::new(42));
let ptr_bool = PtrBool::new(raw_ptr, true).unwrap();

unsafe { assert_eq!(*ptr_bool.as_ref().unwrap(), 42) };
assert_eq!(ptr_bool.as_bool(), true);

unsafe { Box::from_raw(ptr_bool.as_mut_ptr()) };
Source

pub unsafe fn as_ref_unchecked<'a>(&self) -> &'a T

Converts this PtrBool into a reference. Equivalent to &*ptr_bool.as_mut_ptr() in terms of safety.

§Safety

By calling this method you promise that:

  • The wrapped pointer must be properly aligned.

  • This pointer must be dereferenceable in the sense defined in the ptr documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get mutated (except inside UnsafeCell).

If this pointer may be null, use the as_ref method instead.

§Examples
use ptr_bool::PtrBool;

let raw_ptr = Box::into_raw(Box::new(42));
let ptr_bool = PtrBool::new(raw_ptr, true).unwrap();

unsafe { assert_eq!(*ptr_bool.as_ref_unchecked(), 42) };
assert_eq!(ptr_bool.as_bool(), true);

unsafe { Box::from_raw(ptr_bool.as_mut_ptr()) };
Source

pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T>

Converts this PtrBool into a reference.

§Safety

By calling this method you promise that:

  • The wrapped pointer must be properly aligned.

  • This pointer must be dereferenceable in the sense defined in the ptr documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get accessed (read or written) through any other pointer.

If you are completely sure this pointer is not null; then the as_mut_unchecked method can be used instead.

§Examples
use ptr_bool::PtrBool;

let raw_ptr = Box::into_raw(Box::new(42));
let ptr_bool = PtrBool::new(raw_ptr, true).unwrap();

unsafe { assert_eq!(*ptr_bool.as_mut().unwrap(), 42) };
assert_eq!(ptr_bool.as_bool(), true);

unsafe { Box::from_raw(ptr_bool.as_mut_ptr()) };
Source

pub unsafe fn as_mut_unchecked<'a>(&self) -> &'a mut T

Converts this PtrBool into a reference. Equivalent to &mut *ptr_bool.as_mut_ptr() in terms of safety.

§Safety

By calling this method you promise that:

  • The wrapped pointer must be properly aligned.

  • This pointer must be dereferenceable in the sense defined in the ptr documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get accessed (read or written) through any other pointer.

If this pointer may be null, use the as_mut method instead.

§Examples
use ptr_bool::PtrBool;

let raw_ptr = Box::into_raw(Box::new(42));
let ptr_bool = PtrBool::new(raw_ptr, true).unwrap();

unsafe { assert_eq!(*ptr_bool.as_mut_unchecked(), 42) };
assert_eq!(ptr_bool.as_bool(), true);

unsafe { Box::from_raw(ptr_bool.as_mut_ptr()) };

Trait Implementations§

Source§

impl<T: Clone> Clone for PtrBool<T>

Source§

fn clone(&self) -> PtrBool<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for PtrBool<T>

Source§

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

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

impl<T> Display for PtrBool<T>

Source§

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

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

impl<T: PartialEq> PartialEq for PtrBool<T>

Source§

fn eq(&self, other: &PtrBool<T>) -> 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<T> Pointer for PtrBool<T>

Source§

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

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

impl<T: Copy> Copy for PtrBool<T>

Source§

impl<T> StructuralPartialEq for PtrBool<T>

Auto Trait Implementations§

§

impl<T> Freeze for PtrBool<T>

§

impl<T> RefUnwindSafe for PtrBool<T>
where T: RefUnwindSafe,

§

impl<T> !Send for PtrBool<T>

§

impl<T> !Sync for PtrBool<T>

§

impl<T> Unpin for PtrBool<T>

§

impl<T> UnwindSafe for PtrBool<T>
where T: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.