Skip to content

Commit

Permalink
[wip] Support derive(KnownLayout) on DSTs
Browse files Browse the repository at this point in the history
Makes progress towards #29.
  • Loading branch information
jswrenn committed Nov 27, 2023
1 parent eb922ca commit b500286
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 86 deletions.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ pub enum _CastType {
}

impl DstLayout {
/// The minimum possible alignment of a type.
const MIN_ALIGN: NonZeroUsize = match NonZeroUsize::new(1) {
Some(min_align) => min_align,
None => unreachable!(),
};

/// The maximum theoretic possible alignment of a type.
///
/// For compatibility with future Rust versions, this is defined as the
Expand All @@ -419,6 +425,30 @@ impl DstLayout {
None => unreachable!(),
};

/// Constructs a `DstLayout` for a zero-sized type with `repr_align`
/// alignment (or 1). If `repr_align` is provided, then it must be a power
/// of two.
///
/// # Panics
///
/// This function panics if the supplied `repr_align` is not a power of two.
///
/// # Safety
///
/// Unsafe code may assume that the contract of this function is satisfied.
#[doc(hidden)]
#[inline]
pub const fn new_zst(repr_align: Option<NonZeroUsize>) -> DstLayout {
let align = match repr_align {
Some(align) => align,
None => Self::MIN_ALIGN,
};

assert!(align.is_power_of_two());

DstLayout { _align: align, _size_info: SizeInfo::Sized { _size: 0 } }
}

/// Constructs a `DstLayout` which describes `T`.
///
/// # Safety
Expand Down
Loading

0 comments on commit b500286

Please sign in to comment.