diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
commit | 5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch) | |
tree | 35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /vendor/zerofrom/src | |
parent | Adding debian version 1.66.0+dfsg1-1. (diff) | |
download | rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/zerofrom/src')
-rw-r--r-- | vendor/zerofrom/src/lib.rs | 36 | ||||
-rw-r--r-- | vendor/zerofrom/src/macro_impls.rs | 145 | ||||
-rw-r--r-- | vendor/zerofrom/src/zero_from.rs | 128 |
3 files changed, 309 insertions, 0 deletions
diff --git a/vendor/zerofrom/src/lib.rs b/vendor/zerofrom/src/lib.rs new file mode 100644 index 000000000..7b47613fd --- /dev/null +++ b/vendor/zerofrom/src/lib.rs @@ -0,0 +1,36 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! This crate provides [`ZeroFrom`], a trait for converting types in a zero-copy way. +//! +//! See the documentation of [`ZeroFrom`] for more details. + +// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations +#![cfg_attr(not(test), no_std)] +#![cfg_attr( + not(test), + deny( + clippy::indexing_slicing, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::exhaustive_structs, + clippy::exhaustive_enums, + missing_debug_implementations, + ) +)] +// The lifetimes here are important for safety and explicitly writing +// them out is good even when redundant +#![allow(clippy::needless_lifetimes)] + +#[cfg(feature = "alloc")] +extern crate alloc; + +mod macro_impls; +mod zero_from; + +#[cfg(feature = "derive")] +pub use zerofrom_derive::ZeroFrom; + +pub use crate::zero_from::ZeroFrom; diff --git a/vendor/zerofrom/src/macro_impls.rs b/vendor/zerofrom/src/macro_impls.rs new file mode 100644 index 000000000..3bef457e1 --- /dev/null +++ b/vendor/zerofrom/src/macro_impls.rs @@ -0,0 +1,145 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// In this case consistency between impls is more important +// than using pointer casts +#![allow(clippy::transmute_ptr_to_ptr)] + +use crate::ZeroFrom; + +macro_rules! impl_copy_type { + ($ty:ident) => { + impl<'a> ZeroFrom<'a, $ty> for $ty { + #[inline] + fn zero_from(this: &'a Self) -> Self { + // Essentially only works when the struct is fully Copy + *this + } + } + }; +} + +impl_copy_type!(u8); +impl_copy_type!(u16); +impl_copy_type!(u32); +impl_copy_type!(u64); +impl_copy_type!(u128); +impl_copy_type!(usize); +impl_copy_type!(i8); +impl_copy_type!(i16); +impl_copy_type!(i32); +impl_copy_type!(i64); +impl_copy_type!(i128); +impl_copy_type!(isize); +impl_copy_type!(char); +impl_copy_type!(bool); + +// This can be cleaned up once `[T; N]`::each_ref() is stabilized +// https://github.com/rust-lang/rust/issues/76118 +macro_rules! array_zf_impl { + ($n:expr; $($i:expr),+) => { + impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; $n]> for [T; $n] { + fn zero_from(this: &'a [C; $n]) -> Self { + [ + $( + <T as ZeroFrom<C>>::zero_from(&this[$i]) + ),+ + + ] + } + } + } +} + +array_zf_impl!(1; 0); +array_zf_impl!(2; 0, 1); +array_zf_impl!(3; 0, 1, 2); +array_zf_impl!(4; 0, 1, 2, 3); +array_zf_impl!(5; 0, 1, 2, 3, 4); +array_zf_impl!(6; 0, 1, 2, 3, 4, 5); +array_zf_impl!(7; 0, 1, 2, 3, 4, 5, 6); +array_zf_impl!(8; 0, 1, 2, 3, 4, 5, 6, 7); +array_zf_impl!(9; 0, 1, 2, 3, 4, 5, 6, 7, 8); +array_zf_impl!(10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); +array_zf_impl!(11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +array_zf_impl!(12; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); +array_zf_impl!(13; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); +array_zf_impl!(14; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); +array_zf_impl!(15; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); +array_zf_impl!(16; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); + +macro_rules! tuple_zf_impl { + ($(($c:ident, $t:ident, $i:tt)),+) => { + impl<'zf, $($c, $t: ZeroFrom<'zf, $c>),+> ZeroFrom<'zf, ($($c),+)> + for ($($t),+) + { + fn zero_from(other: &'zf ($($c),+)) -> Self { + ( + $(<$t as ZeroFrom<$c>>::zero_from(&other.$i)),+ + ) + } + } + }; +} + +tuple_zf_impl!((C1, T1, 0), (C2, T2, 1)); +tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2)); +tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2), (C4, T4, 3)); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4) +); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4), + (C6, T6, 5) +); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4), + (C6, T6, 5), + (C7, T7, 6) +); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4), + (C6, T6, 5), + (C7, T7, 6), + (C8, T8, 7) +); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4), + (C6, T6, 5), + (C7, T7, 6), + (C8, T8, 7), + (C9, T9, 8) +); +tuple_zf_impl!( + (C1, T1, 0), + (C2, T2, 1), + (C3, T3, 2), + (C4, T4, 3), + (C5, T5, 4), + (C6, T6, 5), + (C7, T7, 6), + (C8, T8, 7), + (C9, T9, 8), + (C10, T10, 9) +); diff --git a/vendor/zerofrom/src/zero_from.rs b/vendor/zerofrom/src/zero_from.rs new file mode 100644 index 000000000..5b89ea5fb --- /dev/null +++ b/vendor/zerofrom/src/zero_from.rs @@ -0,0 +1,128 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +#[cfg(feature = "alloc")] +use alloc::borrow::{Cow, ToOwned}; +#[cfg(feature = "alloc")] +use alloc::string::String; + +/// Trait for types that can be created from a reference to a different type `C` with no allocations, +/// i.e. a zero-copy (zero-alloc) version of "From" +/// +/// A type can be the `ZeroFrom` target of multiple other types. +/// +/// The intention is for `ZeroFrom` to produce a struct from a other with as little work as +/// possible. Although it is technically possible to implement `ZeroFrom` without being +/// zero-copy (using heap allocations), doing so defeats the purpose of `ZeroFrom`. +/// +/// For example, `impl ZeroFrom<C> for Cow<str>` should return a `Cow::Borrowed` pointing at +/// data in the other type `C`, even if the other type is itself fully owned. +/// +/// One can use the [`#[derive(ZeroFrom)]`](zerofrom_derive::ZeroFrom) custom derive to automatically +/// implement this trait. +/// +/// # Examples +/// +/// Implementing `ZeroFrom` on a custom data struct: +/// +/// ``` +/// use std::borrow::Cow; +/// use zerofrom::ZeroFrom; +/// +/// struct MyStruct<'data> { +/// message: Cow<'data, str>, +/// } +/// +/// // Reference from a borrowed version of self +/// impl<'zf> ZeroFrom<'zf, MyStruct<'_>> for MyStruct<'zf> { +/// fn zero_from(other: &'zf MyStruct<'_>) -> Self { +/// MyStruct { +/// message: Cow::Borrowed(&other.message), +/// } +/// } +/// } +/// +/// // Reference from a string slice directly +/// impl<'zf> ZeroFrom<'zf, str> for MyStruct<'zf> { +/// fn zero_from(other: &'zf str) -> Self { +/// MyStruct { +/// message: Cow::Borrowed(other), +/// } +/// } +/// } +/// ``` +pub trait ZeroFrom<'zf, C: ?Sized>: 'zf { + /// Clone the other `C` into a struct that may retain references into `C`. + fn zero_from(other: &'zf C) -> Self; +} + +// Note: The following could be blanket implementations, but that would require constraining the +// blanket `T` on `T: 'static`, which may not be desirable for all downstream users who may wish +// to customize their `ZeroFrom` impl. The blanket implementation may be safe once Rust has +// specialization. + +#[cfg(feature = "alloc")] +impl<'zf> ZeroFrom<'zf, str> for Cow<'zf, str> { + #[inline] + fn zero_from(other: &'zf str) -> Self { + Cow::Borrowed(other) + } +} + +#[cfg(feature = "alloc")] +impl<'zf> ZeroFrom<'zf, String> for Cow<'zf, str> { + #[inline] + fn zero_from(other: &'zf String) -> Self { + Cow::Borrowed(other) + } +} + +impl<'zf> ZeroFrom<'zf, str> for &'zf str { + #[inline] + fn zero_from(other: &'zf str) -> Self { + other + } +} + +#[cfg(feature = "alloc")] +impl<'zf> ZeroFrom<'zf, String> for &'zf str { + #[inline] + fn zero_from(other: &'zf String) -> Self { + other + } +} + +impl<'zf, C, T: ZeroFrom<'zf, C>> ZeroFrom<'zf, Option<C>> for Option<T> { + fn zero_from(other: &'zf Option<C>) -> Self { + other.as_ref().map(|c| <T as ZeroFrom<C>>::zero_from(c)) + } +} + +// These duplicate the functionality from above and aren't quite necessary due +// to deref coercions, however for the custom derive to work, there always needs +// to be `impl ZeroFrom<T> for T`, otherwise it may fail to perform the necessary +// type inference. Deref coercions do not typically work when sufficient generics +// or inference are involved, and the proc macro does not necessarily have +// enough type information to figure this out on its own. +#[cfg(feature = "alloc")] +impl<'zf, B: ToOwned + ?Sized> ZeroFrom<'zf, Cow<'_, B>> for Cow<'zf, B> { + #[inline] + fn zero_from(other: &'zf Cow<'_, B>) -> Self { + Cow::Borrowed(other) + } +} + +impl<'zf> ZeroFrom<'zf, &'_ str> for &'zf str { + #[inline] + fn zero_from(other: &'zf &'_ str) -> &'zf str { + other + } +} + +impl<'zf, T> ZeroFrom<'zf, [T]> for &'zf [T] { + #[inline] + fn zero_from(other: &'zf [T]) -> &'zf [T] { + other + } +} |