Skip to content

Commit abc9d9c

Browse files
committed
add Layout::array_unchecked
1 parent 2bdaf25 commit abc9d9c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- `Dealloc` traits' fallible functions are now a noop for ZSLs and dangling pointers
1515
- All other allocation functions now treat ZSLs as an error.
1616

17+
### Added
18+
19+
- Added `Layout::array_unchecked` constructor
20+
1721
## [0.9.2] - 2026-02-03
1822

1923
### Fixed

src/layout.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Layout {
8080

8181
// could be deduped with repeat*, but stdlib doesn't, and it's logically meaningfully faster, so
8282
// i won't
83-
/// Creates a layout representing an array of `n` `T`.
83+
/// Attempts to create a layout representing an array of `n` count `T`.
8484
///
8585
/// # Errors
8686
///
@@ -94,7 +94,21 @@ impl Layout {
9494

9595
// SAFETY: we just validated that a layout with a size of `T::SZ * n` and alignment of
9696
// `align` will not overflow.
97-
unsafe { Ok(Layout::from_size_align_unchecked(T::SZ * n, T::ALN)) }
97+
unsafe { Ok(Layout::array_unchecked::<T>(n)) }
98+
}
99+
100+
/// Creates a layout representing an array of `n` count `T`.
101+
///
102+
/// # Safety
103+
///
104+
/// The caller must ensure that <code>[T::SZ] * n</code> rounded up to [`T::ALN`] will not
105+
/// exceed [`USIZE_MAX_NO_HIGH_BIT`].
106+
///
107+
/// Additionally, the return value may be unexpected if <code>[T::SZ] * n</code> overflows.
108+
#[must_use]
109+
#[inline]
110+
pub const unsafe fn array_unchecked<T>(n: usize) -> Layout {
111+
Layout::from_size_align_unchecked(T::SZ * n, T::ALN)
98112
}
99113

100114
/// Combines two layouts sequentially, returning the combined layout and the

0 commit comments

Comments
 (0)