zerofrom-0.1.3/.cargo_vcs_info.json0000644000000001540000000000100127000ustar { "git": { "sha1": "e16ba527854ca0edc36695e4e65fdcbc4dccd188" }, "path_in_vcs": "utils/zerofrom" }zerofrom-0.1.3/Cargo.toml0000644000000024560000000000100107050ustar # 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" rust-version = "1.66" name = "zerofrom" version = "0.1.3" authors = ["Manish Goregaokar "] include = [ "data/**/*", "src/**/*", "examples/**/*", "benches/**/*", "tests/**/*", "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-file = "LICENSE" repository = "https://github.com/unicode-org/icu4x" [package.metadata.docs.rs] all-features = true [package.metadata.workspaces] independent = true [dependencies.zerofrom-derive] version = "0.1.1" optional = true default-features = false [features] alloc = [] default = ["alloc"] derive = ["dep:zerofrom-derive"] zerofrom-0.1.3/Cargo.toml.orig000064400000000000000000000015231046102023000143600ustar 00000000000000# 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 ). [package] name = "zerofrom" description = "ZeroFrom trait for constructing" version = "0.1.3" authors = ["Manish Goregaokar "] categories = ["data-structures", "caching", "no-std"] keywords = ["zerocopy", "serialization", "lifetime", "borrow"] edition.workspace = true include.workspace = true license-file.workspace = true repository.workspace = true rust-version.workspace = true [package.metadata.workspaces] independent = true [package.metadata.docs.rs] all-features = true [features] alloc = [] derive = ["dep:zerofrom-derive"] default = ["alloc"] [dependencies] zerofrom-derive = { workspace = true, optional = true} zerofrom-0.1.3/LICENSE000064400000000000000000000041551046102023000125020ustar 00000000000000UNICODE LICENSE V3 COPYRIGHT AND PERMISSION NOTICE Copyright © 2020-2023 Unicode, Inc. NOTICE TO USER: Carefully read the following legal agreement. BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR 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. Permission is hereby granted, free of charge, to any person obtaining a copy of data files and any associated documentation (the "Data Files") or 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. zerofrom-0.1.3/README.md000064400000000000000000000007111046102023000127460ustar 00000000000000# 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). zerofrom-0.1.3/src/lib.rs000064400000000000000000000020741046102023000133760ustar 00000000000000// 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; zerofrom-0.1.3/src/macro_impls.rs000064400000000000000000000070451046102023000151400ustar 00000000000000// 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 { [ $( >::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) ); zerofrom-0.1.3/src/zero_from.rs000064400000000000000000000100071046102023000146250ustar 00000000000000// 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 for Cow` 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> for Option { fn zero_from(other: &'zf Option) -> Self { other.as_ref().map(|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 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, T: ?Sized> ZeroFrom<'zf, &'_ T> for &'zf T { #[inline] fn zero_from(other: &'zf &'_ T) -> &'zf T { other } } impl<'zf, T> ZeroFrom<'zf, [T]> for &'zf [T] { #[inline] fn zero_from(other: &'zf [T]) -> &'zf [T] { other } }