zerocopy/pointer/mod.rs
1// Copyright 2023 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9//! Abstractions over raw pointers.
10
11mod inner;
12#[doc(hidden)]
13pub mod invariant;
14mod ptr;
15mod transmute;
16
17#[doc(hidden)]
18pub(crate) use transmute::*;
19#[doc(hidden)]
20pub use {
21 invariant::{BecauseExclusive, BecauseImmutable, Read},
22 ptr::Ptr,
23};
24
25/// A shorthand for a maybe-valid, maybe-aligned reference. Used as the argument
26/// to [`TryFromBytes::is_bit_valid`].
27///
28/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
29pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unaligned> =
30 Ptr<'a, T, (Aliasing, Alignment, invariant::Initialized)>;
31
32/// Checks if the referent is zeroed.
33pub(crate) fn is_zeroed<T, I>(ptr: Ptr<'_, T, I>) -> bool
34where
35 T: crate::Immutable + crate::KnownLayout,
36 I: invariant::Invariants<Validity = invariant::Initialized>,
37 I::Aliasing: invariant::Reference,
38{
39 ptr.as_bytes::<BecauseImmutable>().as_ref().iter().all(|&byte| byte == 0)
40}