summaryrefslogtreecommitdiffstats
path: root/third_party/rust/zerofrom
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/zerofrom
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/zerofrom')
-rw-r--r--third_party/rust/zerofrom/.cargo-checksum.json1
-rw-r--r--third_party/rust/zerofrom/Cargo.toml54
-rw-r--r--third_party/rust/zerofrom/LICENSE51
-rw-r--r--third_party/rust/zerofrom/README.md9
-rw-r--r--third_party/rust/zerofrom/src/lib.rs36
-rw-r--r--third_party/rust/zerofrom/src/macro_impls.rs145
-rw-r--r--third_party/rust/zerofrom/src/zero_from.rs128
7 files changed, 424 insertions, 0 deletions
diff --git a/third_party/rust/zerofrom/.cargo-checksum.json b/third_party/rust/zerofrom/.cargo-checksum.json
new file mode 100644
index 0000000000..c7a44c2d6c
--- /dev/null
+++ b/third_party/rust/zerofrom/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"72a566dc8765375c7e1b319843a795292a19cba360b8a36e8469271897561e95","LICENSE":"4ad7541d66a407234e2c84902124cef325c29f3e966353efdb800bedb8b8da21","README.md":"dc0e4a45a140001c02818b1f63f9253d5e84c758e46b88f206cacbc5d0401db9","src/lib.rs":"59afe6e1aa74bd1fdab7b1cafa8b5bd1ede141b5c9018d55b76ed395c2f476e3","src/macro_impls.rs":"6b4b69bdc6b94f2abb64da0c2520d3ff74aff7d14e8b4de9e8022d7ddc951f38","src/zero_from.rs":"d589da84905ca5016e91b47672ac8710f035e4da16160d17328fe520f9a6fdce"},"package":"df54d76c3251de27615dfcce21e636c172dafb2549cd7fd93e21c66f6ca6bea2"} \ No newline at end of file
diff --git a/third_party/rust/zerofrom/Cargo.toml b/third_party/rust/zerofrom/Cargo.toml
new file mode 100644
index 0000000000..a0da76d792
--- /dev/null
+++ b/third_party/rust/zerofrom/Cargo.toml
@@ -0,0 +1,54 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2021"
+name = "zerofrom"
+version = "0.1.2"
+authors = ["Manish Goregaokar <manishsmail@gmail.com>"]
+include = [
+ "src/**/*",
+ "examples/**/*",
+ "benches/**/*",
+ "Cargo.toml",
+ "LICENSE",
+ "README.md",
+]
+description = "ZeroFrom trait for constructing"
+readme = "README.md"
+keywords = [
+ "zerocopy",
+ "serialization",
+ "lifetime",
+ "borrow",
+]
+categories = [
+ "data-structures",
+ "caching",
+ "no-std",
+]
+license = "Unicode-DFS-2016"
+repository = "https://github.com/unicode-org/icu4x"
+
+[package.metadata.workspaces]
+independent = true
+
+[package.metadata.docs.rs]
+all-features = true
+
+[dependencies.zerofrom-derive]
+version = "0.1.1"
+optional = true
+
+[features]
+alloc = []
+default = ["alloc"]
+derive = ["dep:zerofrom-derive"]
diff --git a/third_party/rust/zerofrom/LICENSE b/third_party/rust/zerofrom/LICENSE
new file mode 100644
index 0000000000..9858d01abf
--- /dev/null
+++ b/third_party/rust/zerofrom/LICENSE
@@ -0,0 +1,51 @@
+UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE
+
+See Terms of Use <https://www.unicode.org/copyright.html>
+for definitions of Unicode Inc.’s Data Files and Software.
+
+NOTICE TO USER: Carefully read the following legal agreement.
+BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S
+DATA FILES ("DATA FILES"), AND/OR SOFTWARE ("SOFTWARE"),
+YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
+TERMS AND CONDITIONS OF THIS AGREEMENT.
+IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE
+THE DATA FILES OR SOFTWARE.
+
+COPYRIGHT AND PERMISSION NOTICE
+
+Copyright © 1991-2022 Unicode, Inc. All rights reserved.
+Distributed under the Terms of Use in https://www.unicode.org/copyright.html.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of the Unicode data files and any associated documentation
+(the "Data Files") or Unicode software and any associated documentation
+(the "Software") to deal in the Data Files or Software
+without restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, and/or sell copies of
+the Data Files or Software, and to permit persons to whom the Data Files
+or Software are furnished to do so, provided that either
+(a) this copyright and permission notice appear with all copies
+of the Data Files or Software, or
+(b) this copyright and permission notice appear in associated
+Documentation.
+
+THE DATA FILES AND SOFTWARE ARE 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 OF THIRD PARTY RIGHTS.
+IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
+NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 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 THE DATA FILES OR SOFTWARE.
+
+Except as contained in this notice, the name of a copyright holder
+shall not be used in advertising or otherwise to promote the sale,
+use or other dealings in these Data Files or Software without prior
+written authorization of the copyright holder.
+
+—
+
+Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
+ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.
diff --git a/third_party/rust/zerofrom/README.md b/third_party/rust/zerofrom/README.md
new file mode 100644
index 0000000000..d268a347e1
--- /dev/null
+++ b/third_party/rust/zerofrom/README.md
@@ -0,0 +1,9 @@
+# zerofrom [![crates.io](https://img.shields.io/crates/v/zerofrom)](https://crates.io/crates/zerofrom)
+
+This crate provides [`ZeroFrom`], a trait for converting types in a zero-copy way.
+
+See the documentation of [`ZeroFrom`] for more details.
+
+## More Information
+
+For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x).
diff --git a/third_party/rust/zerofrom/src/lib.rs b/third_party/rust/zerofrom/src/lib.rs
new file mode 100644
index 0000000000..7b47613fd3
--- /dev/null
+++ b/third_party/rust/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/third_party/rust/zerofrom/src/macro_impls.rs b/third_party/rust/zerofrom/src/macro_impls.rs
new file mode 100644
index 0000000000..3bef457e11
--- /dev/null
+++ b/third_party/rust/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/third_party/rust/zerofrom/src/zero_from.rs b/third_party/rust/zerofrom/src/zero_from.rs
new file mode 100644
index 0000000000..5b89ea5fb3
--- /dev/null
+++ b/third_party/rust/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
+ }
+}