Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+127
@@ -0,0 +1,127 @@
|
||||
use core::iter::FlatMap;
|
||||
|
||||
/// A specialized version of `core::iter::FlatMap` for mapping over exact-sized
|
||||
/// iterators with a function that returns an array.
|
||||
///
|
||||
/// `ArrayFlatMap` differs from `FlatMap` in that `ArrayFlatMap` implements
|
||||
/// `ExactSizeIterator`. Since the result of `F` always has `LEN` elements, if
|
||||
/// `I` is an exact-sized iterator of length `inner_len` then we know the
|
||||
/// length of the flat-mapped result is `inner_len * LEN`. (The constructor
|
||||
/// verifies that this multiplication doesn't overflow `usize`.)
|
||||
#[derive(Clone)]
|
||||
pub struct ArrayFlatMap<I, Item, F, const LEN: usize> {
|
||||
inner: FlatMap<I, [Item; LEN], F>,
|
||||
remaining: usize,
|
||||
}
|
||||
|
||||
impl<I, Item, F, const LEN: usize> ArrayFlatMap<I, Item, F, LEN>
|
||||
where
|
||||
I: ExactSizeIterator,
|
||||
F: FnMut(I::Item) -> [Item; LEN],
|
||||
{
|
||||
/// Constructs an `ArrayFlatMap` wrapping the given iterator, using the
|
||||
/// given function
|
||||
pub fn new(inner: I, f: F) -> Option<Self> {
|
||||
let remaining = inner.len().checked_mul(LEN)?;
|
||||
let inner = inner.flat_map(f);
|
||||
Some(Self { inner, remaining })
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, Item, F, const LEN: usize> Iterator for ArrayFlatMap<I, Item, F, LEN>
|
||||
where
|
||||
I: Iterator,
|
||||
F: FnMut(I::Item) -> [Item; LEN],
|
||||
{
|
||||
type Item = Item;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let result = self.inner.next();
|
||||
if result.is_some() {
|
||||
self.remaining -= 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Required for implementing `ExactSizeIterator`.
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.remaining, Some(self.remaining))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, Item, F, const LEN: usize> ExactSizeIterator for ArrayFlatMap<I, Item, F, LEN>
|
||||
where
|
||||
I: Iterator,
|
||||
F: FnMut(I::Item) -> [Item; LEN],
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use core::mem::size_of;
|
||||
|
||||
#[test]
|
||||
fn test_array_flat_map() {
|
||||
static TEST_CASES: &[(&[u16], fn(u16) -> [u8; 2], &[u8])] = &[
|
||||
// Empty input
|
||||
(&[], u16::to_be_bytes, &[]),
|
||||
// Non-empty input.
|
||||
(
|
||||
&[0x0102, 0x0304, 0x0506],
|
||||
u16::to_be_bytes,
|
||||
&[1, 2, 3, 4, 5, 6],
|
||||
),
|
||||
// Test with a different mapping function.
|
||||
(
|
||||
&[0x0102, 0x0304, 0x0506],
|
||||
u16::to_le_bytes,
|
||||
&[2, 1, 4, 3, 6, 5],
|
||||
),
|
||||
];
|
||||
TEST_CASES.iter().copied().for_each(|(input, f, expected)| {
|
||||
let mapped = ArrayFlatMap::new(input.iter().copied(), f).unwrap();
|
||||
super::super::test::assert_iterator(mapped, expected);
|
||||
});
|
||||
}
|
||||
|
||||
// Does ArrayFlatMap::new() handle overflow correctly?
|
||||
#[test]
|
||||
fn test_array_flat_map_len_overflow() {
|
||||
struct DownwardCounter {
|
||||
remaining: usize,
|
||||
}
|
||||
impl Iterator for DownwardCounter {
|
||||
type Item = usize;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.remaining > 0 {
|
||||
let result = self.remaining;
|
||||
self.remaining -= 1;
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.remaining, Some(self.remaining))
|
||||
}
|
||||
}
|
||||
impl ExactSizeIterator for DownwardCounter {}
|
||||
|
||||
const MAX: usize = usize::MAX / size_of::<usize>();
|
||||
|
||||
static TEST_CASES: &[(usize, bool)] = &[(MAX, true), (MAX + 1, false)];
|
||||
TEST_CASES.iter().copied().for_each(|(input_len, is_some)| {
|
||||
let inner = DownwardCounter {
|
||||
remaining: input_len,
|
||||
};
|
||||
let mapped = ArrayFlatMap::new(inner, usize::to_be_bytes);
|
||||
assert_eq!(mapped.is_some(), is_some);
|
||||
if let Some(mapped) = mapped {
|
||||
assert_eq!(mapped.len(), input_len * size_of::<usize>());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
// Copyright 2023 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
pub trait ArraySplitMap<I, O, const CN: usize, const ON: usize> {
|
||||
fn array_split_map(self, f: impl Fn([I; CN]) -> O) -> [O; ON];
|
||||
}
|
||||
|
||||
impl<I, O> ArraySplitMap<I, O, 4, 3> for [I; 12] {
|
||||
#[inline]
|
||||
fn array_split_map(self, f: impl Fn([I; 4]) -> O) -> [O; 3] {
|
||||
let [a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3] = self;
|
||||
[
|
||||
f([a0, a1, a2, a3]),
|
||||
f([b0, b1, b2, b3]),
|
||||
f([c0, c1, c2, c3]),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O> ArraySplitMap<I, O, 4, 4> for [I; 16] {
|
||||
#[inline]
|
||||
fn array_split_map(self, f: impl Fn([I; 4]) -> O) -> [O; 4] {
|
||||
let [a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3] = self;
|
||||
[
|
||||
f([a0, a1, a2, a3]),
|
||||
f([b0, b1, b2, b3]),
|
||||
f([c0, c1, c2, c3]),
|
||||
f([d0, d1, d2, d3]),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O> ArraySplitMap<I, O, 4, 8> for [I; 32] {
|
||||
#[inline]
|
||||
fn array_split_map(self, f: impl Fn([I; 4]) -> O) -> [O; 8] {
|
||||
let [a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3, e0, e1, e2, e3, f0, f1, f2, f3, g0, g1, g2, g3, h0, h1, h2, h3] =
|
||||
self;
|
||||
[
|
||||
f([a0, a1, a2, a3]),
|
||||
f([b0, b1, b2, b3]),
|
||||
f([c0, c1, c2, c3]),
|
||||
f([d0, d1, d2, d3]),
|
||||
f([e0, e1, e2, e3]),
|
||||
f([f0, f1, f2, f3]),
|
||||
f([g0, g1, g2, g3]),
|
||||
f([h0, h1, h2, h3]),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O> ArraySplitMap<I, O, 8, 2> for [I; 16] {
|
||||
#[inline]
|
||||
fn array_split_map(self, f: impl Fn([I; 8]) -> O) -> [O; 2] {
|
||||
let [a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3, b4, b5, b6, b7] = self;
|
||||
[
|
||||
f([a0, a1, a2, a3, a4, a5, a6, a7]),
|
||||
f([b0, b1, b2, b3, b4, b5, b6, b7]),
|
||||
]
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
/// Reduces boilerplate for defining error types where we want the compiler to
|
||||
/// optimize for the non-error path by assuming constructing an error is
|
||||
/// unlikely/cold code.
|
||||
///
|
||||
/// WARNING: Every struct/variant must contain some *non-constant* value so
|
||||
/// that the "invariant code" pass of the compiler doesn't recognize the
|
||||
/// constructor as being "invariant code" and optimizing it away;
|
||||
/// although such optimization would be nice to take advantage of, it
|
||||
/// seems to lose the `#[cold]` attribute.
|
||||
///
|
||||
/// Constructor functions ar marked `pub(super)` to ensure that instances can
|
||||
/// only be constructed from within the enclosing module (and its submodules).
|
||||
///
|
||||
/// XXX: #[inline(never)] is required to avoid the (MIR?) optimizer inlining
|
||||
/// away the function call and losing the `#[cold]` attribute in the process.
|
||||
/// We'd otherwise maybe prefer all constructors to be inline.
|
||||
///
|
||||
/// The type is defined in its own submodule `#mod_name` to hide the
|
||||
/// variant/struct constructor, ensuring instances are only constructed
|
||||
/// through the generated `$constructor` functions. The constructor methods
|
||||
/// work around the lack of the ability to mark an enum variant `#[cold]` and
|
||||
/// `#[inline(never)]`.
|
||||
macro_rules! cold_exhaustive_error {
|
||||
// struct
|
||||
{
|
||||
struct $mod_name:ident::$Error:ident with $vis:vis constructor {
|
||||
$field:ident: $ValueType:ty
|
||||
}
|
||||
} => {
|
||||
mod $mod_name {
|
||||
#[allow(unused_imports)]
|
||||
use super::*; // So `$ValueType` is in scope.
|
||||
|
||||
pub struct $Error { #[allow(dead_code)] $field: $ValueType }
|
||||
|
||||
impl $Error {
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
$vis fn new($field: $ValueType) -> Self {
|
||||
Self { $field }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// struct with default constructor visibility.
|
||||
{
|
||||
struct $mod_name:ident::$Error:ident {
|
||||
$field:ident: $ValueType:ty
|
||||
}
|
||||
} => {
|
||||
cold_exhaustive_error! {
|
||||
struct $mod_name::$Error with pub(super) constructor {
|
||||
$field: $ValueType
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// enum
|
||||
{
|
||||
enum $mod_name:ident::$Error:ident {
|
||||
$(
|
||||
$constructor:ident => $Variant:ident($ValueType:ty),
|
||||
)+
|
||||
}
|
||||
} => {
|
||||
mod $mod_name {
|
||||
#[allow(unused_imports)]
|
||||
use super::*; // So `$ValueType` is in scope.
|
||||
|
||||
pub enum $Error {
|
||||
$(
|
||||
$Variant(#[allow(dead_code)] $ValueType)
|
||||
),+
|
||||
}
|
||||
|
||||
impl $Error {
|
||||
$(
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub(super) fn $constructor(value: $ValueType) -> Self {
|
||||
Self::$Variant(value)
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Vendored
+107
@@ -0,0 +1,107 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#![cfg(all(
|
||||
target_vendor = "apple",
|
||||
any(
|
||||
target_os = "ios",
|
||||
target_os = "macos",
|
||||
target_os = "tvos",
|
||||
target_os = "visionos",
|
||||
target_os = "watchos"
|
||||
)
|
||||
))]
|
||||
|
||||
//! Work around lack of `core::ffi::CStr` prior to Rust 1.64, and the lack of
|
||||
//! `const fn` support for `CStr` in later versions.
|
||||
|
||||
#![cfg(all(
|
||||
all(target_arch = "aarch64", target_endian = "little"),
|
||||
target_vendor = "apple"
|
||||
))]
|
||||
|
||||
use core::mem::{align_of, size_of};
|
||||
|
||||
// TODO(MSRV 1.64): Use `core::ffi::c_char`.
|
||||
use libc::c_char;
|
||||
|
||||
// TODO(MSRV 1.64): Replace with `&core::ffi::CStr`.
|
||||
pub struct Ref(&'static [u8]);
|
||||
|
||||
impl Ref {
|
||||
#[inline(always)]
|
||||
pub fn as_ptr(&self) -> *const c_char {
|
||||
const _SAME_ALIGNMENT: () = assert!(align_of::<u8>() == align_of::<c_char>());
|
||||
const _SAME_SIZE: () = assert!(size_of::<u8>() == size_of::<c_char>());
|
||||
|
||||
// It is safe to cast a `*const u8` to a `const c_char` as they are the
|
||||
// same size and alignment.
|
||||
self.0.as_ptr().cast()
|
||||
}
|
||||
|
||||
// SAFETY: Same as `CStr::from_bytes_with_nul_unchecked`.
|
||||
const unsafe fn from_bytes_with_nul_unchecked(value: &'static [u8]) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn unwrap_const_from_bytes_with_nul(value: &'static [u8]) -> Ref {
|
||||
// XXX: We cannot use `unwrap_const` since `Ref`/`CStr` is not `Copy`.
|
||||
match const_from_bytes_with_nul(value) {
|
||||
Some(r) => r,
|
||||
None => panic!("const_from_bytes_with_nul failed"),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(MSRV 1.72): Replace with `CStr::from_bytes_with_nul`.
|
||||
#[inline(always)]
|
||||
const fn const_from_bytes_with_nul(value: &'static [u8]) -> Option<Ref> {
|
||||
const fn const_contains(mut value: &[u8], needle: &u8) -> bool {
|
||||
while let [head, tail @ ..] = value {
|
||||
if *head == *needle {
|
||||
return true;
|
||||
}
|
||||
value = tail;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
// TODO(MSRV 1.69): Use `core::ffi::CStr::from_bytes_until_nul`
|
||||
match value {
|
||||
[before_nul @ .., 0] if !const_contains(before_nul, &0) => {
|
||||
// SAFETY:
|
||||
// * `value` is nul-terminated according to the slice pattern.
|
||||
// * `value` doesn't contain any interior null, by the guard.
|
||||
// TODO(MSRV 1.64): Use `CStr::from_bytes_with_nul_unchecked`
|
||||
Some(unsafe { Ref::from_bytes_with_nul_unchecked(value) })
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::const_from_bytes_with_nul;
|
||||
|
||||
// Bad.
|
||||
const _EMPTY_UNTERMINATED: () = assert!(const_from_bytes_with_nul(b"").is_none());
|
||||
const _EMPTY_DOUBLE_TERMINATED: () = assert!(const_from_bytes_with_nul(b"\0\0").is_none());
|
||||
const _DOUBLE_NUL: () = assert!(const_from_bytes_with_nul(b"\0\0").is_none());
|
||||
const _LEADINGL_NUL: () = assert!(const_from_bytes_with_nul(b"\0a\0").is_none());
|
||||
const _INTERNAL_NUL_UNTERMINATED: () = assert!(const_from_bytes_with_nul(b"\0a").is_none());
|
||||
|
||||
// Good.
|
||||
const _EMPTY_TERMINATED: () = assert!(const_from_bytes_with_nul(b"\0").is_some());
|
||||
const _NONEMPTY: () = assert!(const_from_bytes_with_nul(b"asdf\0").is_some());
|
||||
const _1_CHAR: () = assert!(const_from_bytes_with_nul(b"a\0").is_some());
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
use core::iter::Peekable;
|
||||
|
||||
/// An iterator that skips all leading zeros.
|
||||
///
|
||||
/// When the wrapped iterator is all zeros, then the last item is retained.
|
||||
pub struct LeadingZerosStripped<I>
|
||||
where
|
||||
I: Iterator,
|
||||
{
|
||||
inner: Peekable<I>,
|
||||
}
|
||||
|
||||
impl<I> Clone for LeadingZerosStripped<I>
|
||||
where
|
||||
I: Iterator,
|
||||
Peekable<I>: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> LeadingZerosStripped<I>
|
||||
where
|
||||
I: ExactSizeIterator<Item = u8>,
|
||||
{
|
||||
pub fn new(inner: I) -> Self {
|
||||
let mut len = inner.len();
|
||||
let mut inner = inner.peekable();
|
||||
// Strip all leading zeroes, but don't strip the last byte if all bytes
|
||||
// were zero.
|
||||
while len > 1 && inner.next_if_eq(&0).is_some() {
|
||||
len -= 1;
|
||||
}
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for LeadingZerosStripped<I>
|
||||
where
|
||||
I: Iterator,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.inner.next()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for LeadingZerosStripped<I> where I: ExactSizeIterator {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_leading_zeroes_stripped() {
|
||||
static TEST_CASES: &[(&[u8], &[u8])] = &[
|
||||
(&[], &[]),
|
||||
(&[0], &[0]),
|
||||
(&[0, 1], &[1]),
|
||||
(&[0, 0, 1], &[1]),
|
||||
(&[0, 0, 0, 1], &[1]),
|
||||
(&[1, 0], &[1, 0]),
|
||||
(&[0, 1, 0], &[1, 0]),
|
||||
];
|
||||
TEST_CASES.iter().copied().for_each(|(input, expected)| {
|
||||
let stripped = LeadingZerosStripped::new(input.iter().copied());
|
||||
super::super::test::assert_iterator(stripped, expected);
|
||||
});
|
||||
}
|
||||
}
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use crate::testutil;
|
||||
use core::{marker::PhantomData, mem::size_of};
|
||||
|
||||
/// A ZST that can be added to any type to make the type `!Send`.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct NotSend(PhantomData<*mut ()>);
|
||||
|
||||
impl NotSend {
|
||||
pub const VALUE: Self = Self(PhantomData);
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
const _: () = testutil::compile_time_assert_clone::<NotSend>();
|
||||
#[allow(deprecated)]
|
||||
const _: () = testutil::compile_time_assert_copy::<NotSend>();
|
||||
const _: () = assert!(size_of::<NotSend>() == 0);
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
//! Thread-safe, non-blocking, "first one wins" flavor of `OnceCell`.
|
||||
//!
|
||||
//! If two threads race to initialize a type from the `race` module, they
|
||||
//! don't block, execute initialization function together, but only one of
|
||||
//! them stores the result.
|
||||
//!
|
||||
//! This module does not require `std` feature.
|
||||
//!
|
||||
//! # Atomic orderings
|
||||
//!
|
||||
//! All types in this module use `Acquire` and `Release`
|
||||
//! [atomic orderings](Ordering) for all their operations. While this is not
|
||||
//! strictly necessary for types other than `OnceBox`, it is useful for users as
|
||||
//! it allows them to be certain that after `get` or `get_or_init` returns on
|
||||
//! one thread, any side-effects caused by the setter thread prior to them
|
||||
//! calling `set` or `get_or_init` will be made visible to that thread; without
|
||||
//! it, it's possible for it to appear as if they haven't happened yet from the
|
||||
//! getter thread's perspective. This is an acceptable tradeoff to make since
|
||||
//! `Acquire` and `Release` have very little performance overhead on most
|
||||
//! architectures versus `Relaxed`.
|
||||
|
||||
use core::sync::atomic;
|
||||
|
||||
use atomic::{AtomicUsize, Ordering};
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
/// A thread-safe cell which can be written to only once.
|
||||
pub struct OnceNonZeroUsize {
|
||||
inner: AtomicUsize,
|
||||
}
|
||||
|
||||
impl OnceNonZeroUsize {
|
||||
/// Creates a new empty cell.
|
||||
#[inline]
|
||||
pub const fn new() -> OnceNonZeroUsize {
|
||||
OnceNonZeroUsize {
|
||||
inner: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the underlying value.
|
||||
#[inline]
|
||||
pub fn get(&self) -> Option<NonZeroUsize> {
|
||||
let val = self.inner.load(Ordering::Acquire);
|
||||
NonZeroUsize::new(val)
|
||||
}
|
||||
|
||||
/// Gets the contents of the cell, initializing it with `f` if the cell was
|
||||
/// empty.
|
||||
///
|
||||
/// If several threads concurrently run `get_or_init`, more than one `f` can
|
||||
/// be called. However, all threads will return the same value, produced by
|
||||
/// some `f`.
|
||||
pub fn get_or_init<F>(&self, f: F) -> NonZeroUsize
|
||||
where
|
||||
F: FnOnce() -> NonZeroUsize,
|
||||
{
|
||||
let val = self.inner.load(Ordering::Acquire);
|
||||
match NonZeroUsize::new(val) {
|
||||
Some(it) => it,
|
||||
None => self.init(f),
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn init(&self, f: impl FnOnce() -> NonZeroUsize) -> NonZeroUsize {
|
||||
let mut val = f().get();
|
||||
let exchange = self
|
||||
.inner
|
||||
.compare_exchange(0, val, Ordering::AcqRel, Ordering::Acquire);
|
||||
if let Err(old) = exchange {
|
||||
val = old;
|
||||
}
|
||||
unsafe { NonZeroUsize::new_unchecked(val) }
|
||||
}
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// TODO(MSRV 1.76): Replace with `core::ptr::from_mut`.
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
pub fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
|
||||
r
|
||||
}
|
||||
|
||||
// TODO(MSRV 1.76): Replace with `core::ptr::from_ref`.
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
|
||||
r
|
||||
}
|
||||
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
// Permission is hereby granted, free of charge, to any
|
||||
// person obtaining a copy of this software and associated
|
||||
// documentation files (the "Software"), to deal in the
|
||||
// Software without restriction, including without
|
||||
// limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software
|
||||
// is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice
|
||||
// shall be included in all copies or substantial portions
|
||||
// of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
mod as_chunks;
|
||||
mod as_chunks_mut;
|
||||
|
||||
pub use as_chunks::{as_chunks, AsChunks};
|
||||
pub use as_chunks_mut::{as_chunks_mut, AsChunksMut};
|
||||
|
||||
// TODO(MSRV feature(split_at_checked)): Use `slice::split_at_checked`.
|
||||
//
|
||||
// Note that the libcore version is implemented in terms of
|
||||
// `slice::split_at_unchecked()`, and `slice::split_at()` was changed to be
|
||||
// implemented in terms of `split_at_checked`. For now, we implement this in
|
||||
// terms of `split_at` and rely on the optimizer to eliminate the panic.
|
||||
#[inline(always)]
|
||||
pub fn split_at_checked<T>(slice: &[T], i: usize) -> Option<(&[T], &[T])> {
|
||||
if slice.len() >= i {
|
||||
Some(slice.split_at(i))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(MSRV-1.77): Use `slice::split_first_chunk_mut`.
|
||||
#[inline(always)]
|
||||
pub fn split_first_chunk_mut<T, const N: usize>(
|
||||
slice: &mut [T],
|
||||
) -> Option<(&mut [T; N], &mut [T])> {
|
||||
if slice.len() >= N {
|
||||
let (head, tail) = slice.split_at_mut(N);
|
||||
head.try_into().ok().map(|head| (head, tail))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
// Copyright 2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::AsChunksMut;
|
||||
use core::ops;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_chunks<T, const N: usize>(slice: &[T]) -> (AsChunks<T, N>, &[T]) {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
let len = slice.len() / N;
|
||||
let (multiple_of_n, remainder) = slice.split_at(len * N);
|
||||
(AsChunks(multiple_of_n), remainder)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct AsChunks<'a, T, const N: usize>(&'a [T]);
|
||||
|
||||
impl<'a, T, const N: usize> AsChunks<'a, T, N> {
|
||||
#[inline(always)]
|
||||
pub fn from_ref(value: &'a [T; N]) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_flattened(&self) -> &[T] {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))]
|
||||
#[inline(always)]
|
||||
pub fn as_ptr(&self) -> *const [T; N] {
|
||||
self.0.as_ptr().cast()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len() / N
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> ops::Index<usize> for AsChunks<'_, T, N>
|
||||
where
|
||||
[T]: ops::Index<ops::Range<usize>, Output = [T]>,
|
||||
{
|
||||
type Output = [T; N];
|
||||
|
||||
#[inline(always)]
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
let start = N * index;
|
||||
let slice = &self.0[start..(start + N)];
|
||||
slice.try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, const N: usize> IntoIterator for AsChunks<'a, T, N> {
|
||||
type IntoIter = AsChunksIter<'a, T, N>;
|
||||
type Item = &'a [T; N];
|
||||
|
||||
#[inline(always)]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
AsChunksIter(self.0.chunks_exact(N))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsChunksIter<'a, T, const N: usize>(core::slice::ChunksExact<'a, T>);
|
||||
|
||||
impl<'a, T, const N: usize> Iterator for AsChunksIter<'a, T, N> {
|
||||
type Item = &'a [T; N];
|
||||
|
||||
#[inline(always)]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.next().map(|x| x.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
// `&mut [[T; N]]` is implicitly convertable to `&[[T; N]]` but our types can't
|
||||
// do that.
|
||||
impl<'a, T, const N: usize> From<&'a AsChunksMut<'_, T, N>> for AsChunks<'a, T, N> {
|
||||
#[inline(always)]
|
||||
fn from(as_mut: &'a AsChunksMut<'_, T, N>) -> Self {
|
||||
Self(as_mut.as_flattened())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, const N: usize> From<&'a [T; N]> for AsChunks<'a, T, N> {
|
||||
#[inline(always)]
|
||||
fn from(array: &'a [T; N]) -> Self {
|
||||
Self(array)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: `impl From<AsChunks<'a, T, {2*N}> for AsChunks<'a, T, N>`.
|
||||
impl<'a, T> From<AsChunks<'a, T, 8>> for AsChunks<'a, T, 4> {
|
||||
#[inline(always)]
|
||||
fn from(as_2x: AsChunks<'a, T, 8>) -> Self {
|
||||
Self(as_2x.0)
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
// Copyright 2025 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::AsChunks;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_chunks_mut<T, const N: usize>(slice: &mut [T]) -> (AsChunksMut<T, N>, &mut [T]) {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
let len = slice.len() / N;
|
||||
let (multiple_of_n, remainder) = slice.split_at_mut(len * N);
|
||||
(AsChunksMut(multiple_of_n), remainder)
|
||||
}
|
||||
|
||||
pub struct AsChunksMut<'a, T, const N: usize>(&'a mut [T]);
|
||||
|
||||
impl<T, const N: usize> AsChunksMut<'_, T, N> {
|
||||
#[inline(always)]
|
||||
pub fn as_flattened(&self) -> &[T] {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_flattened_mut(&mut self) -> &mut [T] {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub fn as_ptr(&self) -> *const [T; N] {
|
||||
self.0.as_ptr().cast()
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub fn as_ptr(&self) -> *const [T; N] {
|
||||
self.0.as_ptr().cast()
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub fn as_mut_ptr(&mut self) -> *mut [T; N] {
|
||||
self.0.as_mut_ptr().cast()
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[inline(always)]
|
||||
pub fn as_mut(&mut self) -> AsChunksMut<T, N> {
|
||||
AsChunksMut(self.0)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_ref(&self) -> AsChunks<T, N> {
|
||||
AsChunks::<T, N>::from(self)
|
||||
}
|
||||
|
||||
// Argument moved from runtime argument to `const` argument so that
|
||||
// `CHUNK_LEN * N` is checked at compile time for overflow.
|
||||
#[inline(always)]
|
||||
pub fn chunks_mut<const CHUNK_LEN: usize>(&mut self) -> AsChunksMutChunksMutIter<T, N> {
|
||||
AsChunksMutChunksMutIter(self.0.chunks_mut(CHUNK_LEN * N))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[inline(always)]
|
||||
pub fn split_at_mut(&mut self, mid: usize) -> (AsChunksMut<T, N>, AsChunksMut<T, N>) {
|
||||
let (before, after) = self.0.split_at_mut(mid * N);
|
||||
(AsChunksMut(before), AsChunksMut(after))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsChunksMutChunksMutIter<'a, T, const N: usize>(core::slice::ChunksMut<'a, T>);
|
||||
|
||||
impl<'a, T, const N: usize> Iterator for AsChunksMutChunksMutIter<'a, T, N> {
|
||||
type Item = AsChunksMut<'a, T, N>;
|
||||
|
||||
#[inline(always)]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.next().map(AsChunksMut)
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// Copyright 2024 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
//! Utilities to make dealing with slices less tediuous.
|
||||
|
||||
/// Replaces the first N elements of `a` with the first N elements of `b`, where
|
||||
/// N is `core::cmp::min(a.len(), b.len())`, leaving the rest unchanged.
|
||||
pub fn overwrite_at_start<T: Copy>(a: &mut [T], b: &[T]) {
|
||||
a.iter_mut().zip(b).for_each(|(a, b)| {
|
||||
*a = *b;
|
||||
});
|
||||
}
|
||||
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
pub fn assert_iterator<T>(it: impl ExactSizeIterator<Item = T> + Clone, expected: &[T])
|
||||
where
|
||||
T: Copy + core::fmt::Debug + PartialEq,
|
||||
{
|
||||
// Assert that the cloned iterator is correct.
|
||||
assert_exact_size_iterator(it.clone(), expected);
|
||||
// Assert that the original iterator is correct.
|
||||
assert_exact_size_iterator(it, expected);
|
||||
}
|
||||
|
||||
/// Asserts that `it` adheres to the `ExactSizeIterator` contract.
|
||||
fn assert_exact_size_iterator<T>(mut it: impl ExactSizeIterator<Item = T>, expected: &[T])
|
||||
where
|
||||
T: Copy + core::fmt::Debug + PartialEq,
|
||||
{
|
||||
assert_eq!(it.len(), expected.len());
|
||||
assert_eq!(it.size_hint(), expected.iter().size_hint());
|
||||
|
||||
for i in 0..expected.len() {
|
||||
let len = it.len();
|
||||
assert_eq!(len, expected.len() - i);
|
||||
assert_eq!(it.size_hint(), (len, Some(len)));
|
||||
assert_eq!(it.next(), Some(expected[i]));
|
||||
}
|
||||
|
||||
assert_eq!(it.len(), 0);
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// Copyright 2022 Brian Smith.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
/// Polyfill for `Option::unwrap()` as a const fn; feature `const_option`.
|
||||
/// https://github.com/rust-lang/rust/issues/67441.
|
||||
/// TODO(MSRV): Replace this with `x.unwrap()`.
|
||||
///
|
||||
/// `T: Copy` avoids "constant functions cannot evaluate destructors."
|
||||
pub const fn unwrap_const<T>(x: Option<T>) -> T
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
if let Some(x) = x {
|
||||
x
|
||||
} else {
|
||||
panic!("unwrap_const on `None`");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user