beef-0.5.2/.cargo_vcs_info.json0000644000000001360000000000100117410ustar { "git": { "sha1": "1c2b9a4e7fa410b031f39600e688b53629cb83f0" }, "path_in_vcs": "" }beef-0.5.2/.github/FUNDING.yml000064400000000000000000000001050072674642500137320ustar 00000000000000# These are supported funding model platforms github: [maciejhirsz] beef-0.5.2/.github/workflows/clippy.yaml000064400000000000000000000006410072674642500163430ustar 00000000000000name: Clippy on: [push] jobs: clippy-check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable override: true - name: Install clippy run: rustup component add clippy - uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} args: --all beef-0.5.2/.github/workflows/format.yaml000064400000000000000000000004400072674642500163300ustar 00000000000000name: Format on: [push] jobs: format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable override: true - name: Check Formating run: cargo fmt --all -- --checkbeef-0.5.2/.github/workflows/tests.yaml000064400000000000000000000015650072674642500162130ustar 00000000000000name: Tests on: [push] jobs: miri: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: nightly - name: Run miri run: ci/miri.sh tests: strategy: matrix: rust: - stable - beta - nightly os: - ubuntu-latest - windows-latest - macOS-latest rustflags: - '-C target-cpu=native' - '-C target-cpu=native -C target-feature=-avx2' features: - '' - '--features impl_serde' runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust }} override: true - name: Run tests run: cargo test ${{ matrix.features }} beef-0.5.2/.gitignore000064400000000000000000000000230072674642500125440ustar 00000000000000/target Cargo.lock beef-0.5.2/.travis.yml000064400000000000000000000014040072674642500126710ustar 00000000000000language: rust matrix: include: # Stable - rust: stable script: - cargo test --verbose - cargo test --features impl_serde --verbose # Stable no_std - rust: stable name: "no_std stable" install: - rustup target add thumbv6m-none-eabi script: - cargo build --target thumbv6m-none-eabi --verbose # Beta - rust: beta script: - cargo test --verbose - cargo test --features impl_serde --verbose # Nightly - rust: nightly script: - cargo test --verbose - cargo test --all-features --verbose # Miri - rust: nightly name: "Miri" script: - ci/miri.sh jobs: allow_failures: - rust: nightly fast_finish: true beef-0.5.2/Cargo.toml0000644000000023250000000000100077410ustar # 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 = "2018" name = "beef" version = "0.5.2" authors = ["Maciej Hirsz "] description = "More compact Cow" documentation = "https://docs.rs/beef/" readme = "./README.md" keywords = [ "cow", "borrow", "str", "slice", ] categories = [ "no-std", "memory-management", ] license = "MIT OR Apache-2.0" repository = "https://github.com/maciejhirsz/beef" [profile.bench] opt-level = 3 lto = "fat" codegen-units = 1 debug = false debug-assertions = false [dependencies.serde] version = "1.0.105" features = ["alloc"] optional = true default-features = false [dev-dependencies.serde_derive] version = "1.0.105" [dev-dependencies.serde_json] version = "1.0" [features] const_fn = [] default = [] impl_serde = ["serde"] beef-0.5.2/Cargo.toml.orig000064400000000000000000000015600072674642500134520ustar 00000000000000[package] name = "beef" version = "0.5.2" authors = ["Maciej Hirsz "] edition = "2018" description = "More compact Cow" repository = "https://github.com/maciejhirsz/beef" documentation = "https://docs.rs/beef/" license = "MIT OR Apache-2.0" readme = "./README.md" keywords = ["cow", "borrow", "str", "slice"] categories = ["no-std", "memory-management"] [dependencies] serde = { version = "1.0.105", default-features = false, features = ["alloc"], optional = true } [dev-dependencies] serde_derive = "1.0.105" serde_json = "1.0" [features] default = [] # adds `Cow::const_slice`, the const fn alternative to `Cow::borrowed` for # generic &[T] slices. # requires nightly: https://github.com/rust-lang/rust/issues/57563 const_fn = [] impl_serde = ["serde"] [profile.bench] opt-level = 3 debug = false lto = 'fat' debug-assertions = false codegen-units = 1 beef-0.5.2/LICENSE-APACHE000064400000000000000000000251500072674642500125100ustar 00000000000000 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 2020 Maciej Hirsz 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. beef-0.5.2/LICENSE-MIT000064400000000000000000000021140072674642500122130ustar 00000000000000Copyright (c) 2020 Maciej Hirsz The MIT License (MIT) 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 AUTHORS 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. beef-0.5.2/README.md000064400000000000000000000104520072674642500120420ustar 00000000000000# beef [![Travis shield](https://travis-ci.org/maciejhirsz/beef.svg)](https://travis-ci.org/maciejhirsz/beef) [![Crates.io version shield](https://img.shields.io/crates/v/beef.svg)](https://crates.io/crates/beef) [![Crates.io license shield](https://img.shields.io/crates/l/beef.svg)](https://crates.io/crates/beef) Faster, more compact implementation of `Cow`. **[Changelog](https://github.com/maciejhirsz/beef/releases) -** **[Documentation](https://docs.rs/beef/) -** **[Cargo](https://crates.io/crates/beef) -** **[Repository](https://github.com/maciejhirsz/beef)** ```rust use beef::Cow; let borrowed: Cow = Cow::borrowed("Hello"); let owned: Cow = Cow::owned(String::from("World")); assert_eq!( format!("{} {}!", borrowed, owned), "Hello World!", ); ``` There are two versions of `Cow` exposed by this crate: + `beef::Cow` is 3 words wide: pointer, length, and capacity. It stores the ownership tag in capacity. + `beef::lean::Cow` is 2 words wide, storing length, capacity, and the ownership tag all in one word. Both versions are leaner than the `std::borrow::Cow`: ```rust use std::mem::size_of; const WORD: usize = size_of::(); assert_eq!(size_of::>(), 4 * WORD); assert_eq!(size_of::>(), 3 * WORD); assert_eq!(size_of::>(), 2 * WORD); ``` ## How does it work? The standard library `Cow` is an enum with two variants: ```rust pub enum Cow<'a, B> where B: 'a + ToOwned + ?Sized, { Borrowed(&'a B), Owned(::Owned), } ``` For the most common pairs of values - `&str` and `String`, or `&[u8]` and `Vec` - this means that the entire enum is 4 words wide: ```text Padding | v +-----------+-----------+-----------+-----------+ Borrowed: | Tag | Pointer | Length | XXXXXXXXX | +-----------+-----------+-----------+-----------+ +-----------+-----------+-----------+-----------+ Owned: | Tag | Pointer | Length | Capacity | +-----------+-----------+-----------+-----------+ ``` Instead of being an enum with a tag, `beef::Cow` uses capacity to determine whether the value it's holding is owned (capacity is greater than 0), or borrowed (capacity is 0). `beef::lean::Cow` goes even further and puts length and capacity on a single 64 word. ```text +-----------+-----------+-----------+ beef::Cow | Pointer | Length | Capacity? | +-----------+-----------+-----------+ +-----------+-----------+ beef::lean::Cow | Pointer | Cap | Len | +-----------+-----------+ ``` Any owned `Vec` or `String` that has 0 capacity is effectively treated as a borrowed value. Since having no capacity means there is no actual allocation behind the pointer, this is safe. ## Benchmarks ``` cargo +nightly bench ``` Microbenchmarking obtaining a `&str` reference is rather flaky and you can have widely different results. In general the following seems to hold true: + `beef::Cow` and `beef::lean::Cow` are faster than `std::borrow::Cow` at obtaining a reference `&T`. This makes sense since we avoid the enum tag branching. + The 3-word `beef::Cow` is faster at creating borrowed variants, but slower at creating owned variants than `std::borrow::Cow`. + The 2-word `beef::lean::Cow` is faster at both. ``` running 9 tests test beef_as_ref ... bench: 57 ns/iter (+/- 15) test beef_create ... bench: 135 ns/iter (+/- 5) test beef_create_mixed ... bench: 659 ns/iter (+/- 52) test lean_beef_as_ref ... bench: 50 ns/iter (+/- 2) test lean_beef_create ... bench: 77 ns/iter (+/- 3) test lean_beef_create_mixed ... bench: 594 ns/iter (+/- 52) test std_as_ref ... bench: 70 ns/iter (+/- 6) test std_create ... bench: 142 ns/iter (+/- 7) test std_create_mixed ... bench: 663 ns/iter (+/- 32) ``` ## License This crate is distributed under the terms of both the MIT license and the Apache License (Version 2.0). Choose whichever one works best for you. See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details. beef-0.5.2/benches/bench.rs000064400000000000000000000107100072674642500136140ustar 00000000000000#![feature(test)] extern crate beef; extern crate test; use std::borrow::{Cow as StdCow, ToOwned}; use test::{black_box, Bencher}; const NTH_WORD: usize = 4; static TEXT: &str = "In less than a half-hour, Joe had distributed ninety-two paper cups of tomato juice containing AUM, the drug that promised to turn neophobes into neophiles. He stood in Pioneer Court, just north of the Michigan Avenue Bridge, at a table from which hung a poster reading FREE TOMATO JUICE. Each person who took a cupful was invited to fill out a short questionnaire and leave it in a box on Joe's table. However, Joe explained, the questionnaire was optional, and anyone who wanted to drink the tomato juice and run was welcome to do so."; #[bench] fn beef_create(b: &mut Bencher) { use beef::Cow; let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let cow_words: Vec> = words.iter().copied().map(Cow::borrowed).collect(); black_box(cow_words) }); } #[bench] fn beef_create_mixed(b: &mut Bencher) { use beef::Cow; let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let cow_words: Vec> = words .iter() .copied() .map(|word| { if word.len() % NTH_WORD == 0 { Cow::owned(word.to_owned()) } else { Cow::borrowed(word) } }) .collect(); black_box(cow_words) }); } #[bench] fn beef_as_ref(b: &mut Bencher) { use beef::Cow; let cow_words: Vec<_> = TEXT .split_whitespace() .map(|word| { if word.len() % NTH_WORD == 0 { Cow::owned(word.to_owned()) } else { Cow::borrowed(word) } }) .collect(); b.iter(|| { for word in cow_words.iter() { let word: &str = word.as_ref(); black_box(word); } }); } #[bench] fn lean_beef_create(b: &mut Bencher) { use beef::lean::Cow; let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let cow_words: Vec> = words.iter().copied().map(Cow::borrowed).collect(); black_box(cow_words) }); } #[bench] fn lean_beef_create_mixed(b: &mut Bencher) { use beef::lean::Cow; let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let cow_words: Vec> = words .iter() .copied() .map(|word| { if word.len() % NTH_WORD == 0 { Cow::owned(word.to_owned()) } else { Cow::borrowed(word) } }) .collect(); black_box(cow_words) }); } #[bench] fn lean_beef_as_ref(b: &mut Bencher) { use beef::lean::Cow; let cow_words: Vec<_> = TEXT .split_whitespace() .map(|word| { if word.len() % NTH_WORD == 0 { Cow::owned(word.to_owned()) } else { Cow::borrowed(word) } }) .collect(); b.iter(|| { for word in cow_words.iter() { let word: &str = word.as_ref(); black_box(word); } }); } #[bench] fn std_create(b: &mut Bencher) { let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let stdcow_words: Vec> = words.iter().copied().map(StdCow::Borrowed).collect(); black_box(stdcow_words) }); } #[bench] fn std_create_mixed(b: &mut Bencher) { let words: Vec<_> = TEXT.split_whitespace().collect(); b.iter(|| { let stdcow_words: Vec> = words .iter() .copied() .map(|word| { if word.len() % NTH_WORD == 0 { StdCow::Owned(word.to_owned()) } else { StdCow::Borrowed(word) } }) .collect(); black_box(stdcow_words) }); } #[bench] fn std_as_ref(b: &mut Bencher) { let stdcow_words: Vec<_> = TEXT .split_whitespace() .map(|word| { if word.len() % NTH_WORD == 0 { StdCow::Owned(word.to_owned()) } else { StdCow::Borrowed(word) } }) .collect(); b.iter(|| { for word in stdcow_words.iter() { let word: &str = word.as_ref(); black_box(word); } }); } beef-0.5.2/ci/miri.sh000075500000000000000000000005530072674642500124560ustar 00000000000000#!/bin/sh set -e MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri) echo "Installing latest nightly with Miri: $MIRI_NIGHTLY" rustup set profile minimal rustup default "$MIRI_NIGHTLY" rustup component add miri cargo miri setup MIRIFLAGS='-Zmiri-strict-provenance' cargo miri test --all-features beef-0.5.2/src/generic.rs000064400000000000000000000317260072674642500133430ustar 00000000000000//! This module contains the actual, albeit generic, implementaiton of the `Cow`, //! and the traits that are available to it. use alloc::borrow::{Borrow, Cow as StdCow}; use alloc::string::String; use alloc::vec::Vec; use core::cmp::Ordering; use core::fmt; use core::hash::{Hash, Hasher}; use core::marker::PhantomData; use core::mem::ManuallyDrop; use core::ptr::NonNull; #[cfg(target_pointer_width = "64")] use crate::lean::internal::Lean; use crate::traits::{Beef, Capacity}; use crate::wide::internal::Wide; /// A clone-on-write smart pointer, mostly compatible with [`std::borrow::Cow`](https://doc.rust-lang.org/std/borrow/enum.Cow.html). /// /// This type is using a generic `U: Capacity`. Use either [`beef::Cow`](../type.Cow.html) or [`beef::lean::Cow`](../lean/type.Cow.html) in your code. pub struct Cow<'a, T: Beef + ?Sized + 'a, U: Capacity> { /// Pointer to data ptr: NonNull, /// This usize contains length, but it may contain other /// information pending on impl of `Capacity`, and must therefore /// always go through `U::len` or `U::unpack` fat: usize, /// Capacity field. For `beef::lean::Cow` this is 0-sized! cap: U::Field, /// Lifetime marker marker: PhantomData<&'a T>, } impl Cow<'_, T, U> where T: Beef + ?Sized, U: Capacity, { /// Owned data. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// let owned: Cow = Cow::owned("I own my content".to_string()); /// ``` #[inline] pub fn owned(val: T::Owned) -> Self { let (ptr, fat, cap) = T::owned_into_parts::(val); Cow { ptr, fat, cap, marker: PhantomData, } } } impl<'a, T, U> Cow<'a, T, U> where T: Beef + ?Sized, U: Capacity, { /// Borrowed data. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// let borrowed: Cow = Cow::borrowed("I'm just a borrow"); /// ``` #[inline] pub fn borrowed(val: &'a T) -> Self { let (ptr, fat, cap) = T::ref_into_parts::(val); Cow { ptr, fat, cap, marker: PhantomData, } } /// Extracts the owned data. /// /// Clones the data if it is not already owned. #[inline] pub fn into_owned(self) -> T::Owned { let cow = ManuallyDrop::new(self); match cow.capacity() { Some(capacity) => unsafe { T::owned_from_parts::(cow.ptr, cow.fat, capacity) }, None => unsafe { &*T::ref_from_parts::(cow.ptr, cow.fat) }.to_owned(), } } /// Extracts borrowed data. /// /// Panics: If the data is owned. #[inline] pub fn unwrap_borrowed(self) -> &'a T { if self.capacity().is_some() { panic!("Can not turn owned beef::Cow into a borrowed value") } unsafe { &*T::ref_from_parts::(self.ptr, self.fat) } } /// Returns `true` if data is borrowed or had no capacity. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// let borrowed: Cow = Cow::borrowed("Borrowed"); /// let no_capacity: Cow = Cow::owned(String::new()); /// let owned: Cow = Cow::owned(String::from("Owned")); /// /// assert_eq!(borrowed.is_borrowed(), true); /// assert_eq!(no_capacity.is_borrowed(), true); /// assert_eq!(owned.is_borrowed(), false); /// ``` #[inline] pub fn is_borrowed(&self) -> bool { self.capacity().is_none() } /// Returns `true` if data is owned and has non-0 capacity. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// let borrowed: Cow = Cow::borrowed("Borrowed"); /// let no_capacity: Cow = Cow::owned(String::new()); /// let owned: Cow = Cow::owned(String::from("Owned")); /// /// assert_eq!(borrowed.is_owned(), false); /// assert_eq!(no_capacity.is_owned(), false); /// assert_eq!(owned.is_owned(), true); /// ``` #[inline] pub fn is_owned(&self) -> bool { self.capacity().is_some() } /// Internal convenience method for casting `ptr` into a `&T` #[inline] fn borrow(&self) -> &T { unsafe { &*T::ref_from_parts::(self.ptr, self.fat) } } #[inline] fn capacity(&self) -> Option { U::maybe(self.fat, self.cap) } } impl<'a> Cow<'a, str, Wide> { /// Borrowed data. /// /// This is functionally identical to [`borrow`](./generic/struct.Cow.html#method.borrow). /// We use impl specialization to allow this function to be `const`. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// const HELLO: Cow = Cow::const_str("Hello"); /// ``` pub const fn const_str(val: &'a str) -> Self { Cow { // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ptr: unsafe { NonNull::new_unchecked(val.as_ptr() as *mut u8) }, fat: val.len(), cap: None, marker: PhantomData, } } } #[cfg(target_pointer_width = "64")] impl<'a> Cow<'a, str, Lean> { /// Borrowed data. /// /// This is functionally identical to [`borrow`](./generic/struct.Cow.html#method.borrow). /// We use impl specialization to allow this function to be `const`. /// /// # Example /// /// ```rust /// use beef::lean::Cow; /// /// const HELLO: Cow = Cow::const_str("Hello"); /// ``` pub const fn const_str(val: &'a str) -> Self { Cow { // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ptr: unsafe { NonNull::new_unchecked(val.as_ptr() as *mut u8) }, fat: Lean::mask_len(val.len()), cap: Lean, marker: PhantomData, } } } // This requires nightly: // https://github.com/rust-lang/rust/issues/57563 #[cfg(feature = "const_fn")] impl<'a, T> Cow<'a, [T], Wide> where T: Clone, { /// Borrowed data. /// /// This is functionally identical to [`borrow`](./generic/struct.Cow.html#method.borrow). /// We use impl specialization to allow this function to be `const`. /// /// # Example /// /// ```rust /// use beef::Cow; /// /// const HELLO: Cow<[u8]> = Cow::const_slice(&[1, 2, 3]); /// ``` pub const fn const_slice(val: &'a [T]) -> Self { Cow { // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ptr: unsafe { NonNull::new_unchecked(val.as_ptr() as *mut T) }, fat: val.len(), cap: None, marker: PhantomData, } } } // This requires nightly: // https://github.com/rust-lang/rust/issues/57563 #[cfg(all(feature = "const_fn", target_pointer_width = "64"))] impl<'a, T> Cow<'a, [T], Lean> where T: Clone, { /// Borrowed data. /// /// This i functionally identical to [`borrow`](./generic/struct.Cow.html#method.borrow). /// We use impl specialization to allow this function to be `const`. /// /// # Example /// /// ```rust /// use beef::lean::Cow; /// /// const HELLO: Cow<[u8]> = Cow::const_slice(&[1, 2, 3]); /// ``` pub const fn const_slice(val: &'a [T]) -> Self { Cow { // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ptr: unsafe { NonNull::new_unchecked(val.as_ptr() as *mut T) }, fat: Lean::mask_len(val.len()), cap: Lean, marker: PhantomData, } } } impl Hash for Cow<'_, T, U> where T: Hash + Beef + ?Sized, U: Capacity, { #[inline] fn hash(&self, state: &mut H) { self.borrow().hash(state) } } impl<'a, T, U> Default for Cow<'a, T, U> where T: Beef + ?Sized, U: Capacity, &'a T: Default, { #[inline] fn default() -> Self { Cow::borrowed(Default::default()) } } impl Eq for Cow<'_, T, U> where T: Eq + Beef + ?Sized, U: Capacity, { } impl PartialOrd> for Cow<'_, A, U> where A: Beef + ?Sized + PartialOrd, B: Beef + ?Sized, U: Capacity, V: Capacity, { #[inline] fn partial_cmp(&self, other: &Cow<'_, B, V>) -> Option { PartialOrd::partial_cmp(self.borrow(), other.borrow()) } } impl Ord for Cow<'_, T, U> where T: Ord + Beef + ?Sized, U: Capacity, { #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(self.borrow(), other.borrow()) } } impl<'a, T, U> From<&'a T> for Cow<'a, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn from(val: &'a T) -> Self { Cow::borrowed(val) } } impl From for Cow<'_, str, U> where U: Capacity, { #[inline] fn from(s: String) -> Self { Cow::owned(s) } } impl From> for Cow<'_, [T], U> where T: Clone, U: Capacity, { #[inline] fn from(v: Vec) -> Self { Cow::owned(v) } } impl Drop for Cow<'_, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn drop(&mut self) { if let Some(capacity) = self.capacity() { unsafe { T::owned_from_parts::(self.ptr, self.fat, capacity) }; } } } impl<'a, T, U> Clone for Cow<'a, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn clone(&self) -> Self { match self.capacity() { Some(_) => Cow::owned(self.borrow().to_owned()), None => Cow { ..*self }, } } } impl core::ops::Deref for Cow<'_, T, U> where T: Beef + ?Sized, U: Capacity, { type Target = T; #[inline] fn deref(&self) -> &T { self.borrow() } } impl AsRef for Cow<'_, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn as_ref(&self) -> &T { self.borrow() } } impl Borrow for Cow<'_, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn borrow(&self) -> &T { self.borrow() } } impl<'a, T, U> From> for Cow<'a, T, U> where T: Beef + ?Sized, U: Capacity, { #[inline] fn from(stdcow: StdCow<'a, T>) -> Self { match stdcow { StdCow::Borrowed(v) => Self::borrowed(v), StdCow::Owned(v) => Self::owned(v), } } } impl<'a, T, U> From> for StdCow<'a, T> where T: Beef + ?Sized, U: Capacity, { #[inline] fn from(cow: Cow<'a, T, U>) -> Self { let cow = ManuallyDrop::new(cow); match cow.capacity() { Some(capacity) => { StdCow::Owned(unsafe { T::owned_from_parts::(cow.ptr, cow.fat, capacity) }) } None => StdCow::Borrowed(unsafe { &*T::ref_from_parts::(cow.ptr, cow.fat) }), } } } impl PartialEq> for Cow<'_, A, U> where A: Beef + ?Sized, B: Beef + ?Sized, U: Capacity, V: Capacity, A: PartialEq, { fn eq(&self, other: &Cow) -> bool { self.borrow() == other.borrow() } } macro_rules! impl_eq { ($($(@for< $bounds:tt >)? $ptr:ty => $([$($deref:tt)+])? <$with:ty>,)*) => {$( impl PartialEq<$with> for Cow<'_, $ptr, U> where U: Capacity, $( $bounds: Clone + PartialEq, )* { #[inline] fn eq(&self, other: &$with) -> bool { self.borrow() == $($($deref)*)* other } } impl PartialEq> for $with where U: Capacity, $( $bounds: Clone + PartialEq, )* { #[inline] fn eq(&self, other: &Cow<$ptr, U>) -> bool { $($($deref)*)* self == other.borrow() } } )*}; } impl_eq! { str => , str => [*]<&str>, str => , @for [T] => <[T]>, @for [T] => [*]<&[T]>, @for [T] => [&**]>, } impl fmt::Debug for Cow<'_, T, U> where T: Beef + fmt::Debug + ?Sized, U: Capacity, { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.borrow().fmt(f) } } impl fmt::Display for Cow<'_, T, U> where T: Beef + fmt::Display + ?Sized, U: Capacity, { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.borrow().fmt(f) } } // Safety: Same bounds as `std::borrow::Cow`. unsafe impl Sync for Cow<'_, T, U> where U: Capacity, T: Beef + Sync + ?Sized, T::Owned: Sync, { } unsafe impl Send for Cow<'_, T, U> where U: Capacity, T: Beef + Sync + ?Sized, T::Owned: Send, { } impl Unpin for Cow<'_, T, U> where U: Capacity, T: Beef + ?Sized, T::Owned: Unpin, { } beef-0.5.2/src/lean.rs000064400000000000000000000032060072674642500126360ustar 00000000000000//! Namespace containing the 2-word `Cow` implementation. use crate::traits::Capacity; /// Faster, 2-word `Cow`. This version is available only on 64-bit architecture, /// and it puts both capacity and length together in a fat pointer. Both length and capacity /// is limited to 32 bits. /// /// # Panics /// /// [`Cow::owned`](../generic/struct.Cow.html#method.owned) will panic if capacity is larger than `u32::max_size()`. Use the /// top level `beef::Cow` if you wish to avoid this problem. pub type Cow<'a, T> = crate::generic::Cow<'a, T, Lean>; pub(crate) mod internal { #[derive(Clone, Copy, PartialEq, Eq)] pub struct Lean; } use internal::Lean; const MASK_LO: usize = u32::MAX as usize; const MASK_HI: usize = !MASK_LO; impl Lean { #[inline] pub const fn mask_len(len: usize) -> usize { len & MASK_LO } } impl Capacity for Lean { type Field = Lean; type NonZero = Lean; #[inline] fn len(fat: usize) -> usize { fat & MASK_LO } #[inline] fn empty(len: usize) -> (usize, Lean) { (len & MASK_LO, Lean) } #[inline] fn store(len: usize, capacity: usize) -> (usize, Lean) { if capacity & MASK_HI != 0 { panic!("beef::lean::Cow: Capacity out of bounds"); } let fat = ((capacity & MASK_LO) << 32) | (len & MASK_LO); (fat, Lean) } #[inline] fn unpack(fat: usize, _: Lean) -> (usize, usize) { (fat & MASK_LO, (fat & MASK_HI) >> 32) } #[inline] fn maybe(fat: usize, _: Lean) -> Option { if fat & MASK_HI != 0 { Some(Lean) } else { None } } } beef-0.5.2/src/lib.rs000064400000000000000000000171450072674642500124740ustar 00000000000000//! Faster, more compact implementation of `Cow`. //! //! **[Changelog](https://github.com/maciejhirsz/beef/releases) -** //! **[Cargo](https://crates.io/crates/beef) -** //! **[Repository](https://github.com/maciejhirsz/beef)** //! //! ```rust //! use beef::Cow; //! //! let borrowed: Cow = Cow::borrowed("Hello"); //! let owned: Cow = Cow::owned(String::from("World")); //! //! assert_eq!( //! format!("{} {}!", borrowed, owned), //! "Hello World!", //! ); //! ``` //! //! There are two versions of `Cow` exposed by this crate: //! //! + `beef::Cow` is 3 words wide: pointer, length, and capacity. It stores the ownership tag in capacity. //! + `beef::lean::Cow` is 2 words wide, storing length, capacity, and the ownership tag all in one word. //! //! Both versions are leaner than the `std::borrow::Cow`: //! //! ```rust //! use std::mem::size_of; //! //! const WORD: usize = size_of::(); //! //! assert_eq!(size_of::>(), 4 * WORD); //! assert_eq!(size_of::>(), 3 * WORD); //! //! // Lean variant is two words on 64-bit architecture //! #[cfg(target_pointer_width = "64")] //! assert_eq!(size_of::>(), 2 * WORD); //! ``` #![cfg_attr(feature = "const_fn", feature(const_fn_trait_bound))] #![warn(missing_docs)] #![cfg_attr(not(test), no_std)] extern crate alloc; mod traits; mod wide; #[cfg(feature = "impl_serde")] mod serde; pub mod generic; #[cfg(target_pointer_width = "64")] pub mod lean; #[cfg(not(target_pointer_width = "64"))] pub mod lean { /// Re-exports 3-word Cow for non-64-bit targets pub use super::wide::Cow; } pub use wide::Cow; #[rustfmt::skip] macro_rules! test { ($tmod:ident => $cow:path) => { #[cfg(test)] mod $tmod { use $cow; #[test] fn borrowed_str() { let s = "Hello World"; let c = Cow::borrowed(s); assert_eq!(s, c); assert_eq!(s, c.as_ref()); assert_eq!(s, &*c); } #[test] fn owned_string() { let s = String::from("Hello World"); let c: Cow = Cow::owned(s.clone()); assert_eq!(s, c); } #[test] fn into_owned() { let hello = "Hello World"; let borrowed = Cow::borrowed(hello); let owned: Cow = Cow::owned(String::from(hello)); assert_eq!(borrowed.into_owned(), hello); assert_eq!(owned.into_owned(), hello); } #[test] fn borrowed_slice() { let s: &[_] = &[1, 2, 42]; let c = Cow::borrowed(s); assert_eq!(s, c); assert_eq!(s, c.as_ref()); assert_eq!(s, &*c); } #[test] fn owned_slice() { let s = vec![1, 2, 42]; let c: Cow<[_]> = Cow::owned(s.clone()); assert_eq!(s, c); } #[test] fn into_owned_vec() { let hello: &[u8] = b"Hello World"; let borrowed = Cow::borrowed(hello); let owned: Cow<[u8]> = Cow::owned(hello.to_vec()); assert_eq!(borrowed.into_owned(), hello); assert_eq!(owned.into_owned(), hello); } #[test] fn hash() { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; let slice = "Hello World!"; let borrowed = Cow::borrowed(slice); let owned: Cow = Cow::owned(slice.to_owned()); let hash1 = { let mut hasher = DefaultHasher::default(); slice.hash(&mut hasher); hasher.finish() }; let hash2 = { let mut hasher = DefaultHasher::default(); borrowed.hash(&mut hasher); hasher.finish() }; let hash3 = { let mut hasher = DefaultHasher::default(); owned.hash(&mut hasher); hasher.finish() }; assert_eq!(hash1, hash2); assert_eq!(hash1, hash3); assert_eq!(hash2, hash3); } #[test] fn ord_and_partial_ord() { use std::cmp::Ordering; macro_rules! generate_order_tests { ( $f:tt => $order:expr => $left:expr, $right:expr ) => { assert_eq!( Cow::::borrowed($left).$f(&Cow::::borrowed($right)), $order ); assert_eq!( Cow::::owned($left.to_owned()) .$f(&Cow::::borrowed($right)), $order ); assert_eq!( Cow::::borrowed($left) .$f(&Cow::::owned($right.to_owned())), $order ); assert_eq!( Cow::::owned($left.to_owned()) .$f(&Cow::::owned($right.to_owned())), $order ); } } generate_order_tests!(partial_cmp => Some(Ordering::Equal) => "a", "a"); generate_order_tests!(partial_cmp => Some(Ordering::Less) => "a", "b"); generate_order_tests!(partial_cmp => Some(Ordering::Greater) => "b", "a"); generate_order_tests!(cmp => Ordering::Equal => "a", "a"); generate_order_tests!(cmp => Ordering::Less => "a", "b"); generate_order_tests!(cmp => Ordering::Greater => "b", "a"); } #[test] fn from_std_cow() { let std = std::borrow::Cow::Borrowed("Hello World"); let beef = Cow::from(std.clone()); assert_eq!(&*std, &*beef); } #[test] fn unwrap_borrowed() { let borrowed = Cow::borrowed("Hello"); assert_eq!(borrowed.unwrap_borrowed(), "Hello"); } #[test] #[should_panic] fn unwrap_owned() { let borrowed: Cow = Cow::owned("Hello".to_string()); borrowed.unwrap_borrowed(); } #[test] fn stress_test_owned() { let mut expected = String::from("Hello... "); let mut cow: Cow = Cow::borrowed("Hello... "); #[cfg(not(miri))] let iterations = 1024; #[cfg(miri)] let iterations = 10; for i in 0..iterations { if i % 3 == 0 { let old = cow; cow = old.clone(); std::mem::drop(old); } let mut owned = cow.into_owned(); expected.push_str("Hello?.. "); owned.push_str("Hello?.. "); cow = owned.into(); } assert_eq!(expected, cow.into_owned()); } #[test] fn const_fn_str() { const HELLO: Cow = Cow::const_str("Hello"); assert_eq!(&*HELLO, "Hello"); } #[test] #[cfg(feature = "const_fn")] fn const_fn_slice() { const FOO: Cow<[u8]> = Cow::const_slice(b"bar"); assert_eq!(&*FOO, b"bar"); } #[test] fn default_str() { let empty: Cow = Default::default(); assert_eq!(&*empty, ""); } #[test] fn default_slice() { let empty: Cow<[u8]> = Default::default(); assert_eq!(&*empty, b""); } } } } test!(test_wide => crate::wide::Cow); test!(test_lean => crate::lean::Cow); beef-0.5.2/src/serde.rs000064400000000000000000000065520072674642500130300ustar 00000000000000use alloc::{borrow::ToOwned, string::String}; use core::{fmt, marker::PhantomData}; use serde::de::{self, Deserialize, Deserializer, Visitor}; use serde::ser::{Serialize, Serializer}; use crate::generic::Cow; use crate::traits::internal::{Beef, Capacity}; impl Serialize for Cow<'_, T, U> where T: Beef + Serialize + ?Sized, U: Capacity, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { T::serialize(self.as_ref(), serializer) } } struct CowVisitor<'de, 'a, T: Beef + ?Sized, U: Capacity>( PhantomData (&'de T, Cow<'a, T, U>)>, ); impl<'de, 'a, U> Visitor<'de> for CowVisitor<'de, 'a, str, U> where 'de: 'a, U: Capacity, { type Value = Cow<'a, str, U>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_borrowed_str(self, value: &'de str) -> Result where E: de::Error, { Ok(Cow::borrowed(value)) } fn visit_str(self, value: &str) -> Result where E: de::Error, { Ok(Cow::owned(value.to_owned())) } fn visit_string(self, value: String) -> Result where E: de::Error, { Ok(Cow::owned(value)) } } impl<'de, 'a, U> Deserialize<'de> for Cow<'a, str, U> where 'de: 'a, U: Capacity, { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_str(CowVisitor::<'de, 'a, str, U>(PhantomData)) } } impl<'de, 'a, T, U> Deserialize<'de> for Cow<'a, [T], U> where [T]: Beef, U: Capacity, <[T] as ToOwned>::Owned: Deserialize<'de>, { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { <[T] as ToOwned>::Owned::deserialize(deserializer).map(Cow::owned) } } #[cfg(test)] mod tests { use serde_derive::{Deserialize, Serialize}; #[test] fn wide_cow_de() { use crate::Cow; #[derive(Serialize, Deserialize)] struct Test<'a> { #[serde(borrow)] foo: Cow<'a, str>, bar: Cow<'a, str>, } let json = r#"{"foo":"Hello","bar":"\tWorld!"}"#; let test: Test = serde_json::from_str(json).unwrap(); assert_eq!(test.foo, "Hello"); assert_eq!(test.bar, "\tWorld!"); assert!(test.foo.is_borrowed()); assert!(test.bar.is_owned()); let out = serde_json::to_string(&test).unwrap(); assert_eq!(json, out); } #[test] fn wide_cow_direct() { use crate::Cow; let json = r#""foo""#; let cow: Cow = serde_json::from_str(json).unwrap(); assert_eq!(cow, "foo"); assert!(cow.is_borrowed()); let json = r#""\tfoo""#; let cow: Cow = serde_json::from_str(json).unwrap(); assert_eq!(cow, "\tfoo"); assert!(cow.is_owned()); } #[test] fn wide_cow_direct_bytes() { use crate::Cow; let json = r#"[102, 111, 111]"#; let cow: Cow<[u8]> = serde_json::from_str(json).unwrap(); assert_eq!(cow, &b"foo"[..]); // We need to stay generic over `[T]`, so no specialization for byte slices assert!(cow.is_owned()); } } beef-0.5.2/src/traits.rs000064400000000000000000000126340072674642500132320ustar 00000000000000pub(crate) use internal::Beef; pub(crate) use internal::Capacity; pub(crate) mod internal { use alloc::borrow::ToOwned; use alloc::string::String; use alloc::vec::Vec; use core::mem::ManuallyDrop; use core::ptr::{slice_from_raw_parts, NonNull}; pub trait Capacity { type Field: Copy; type NonZero: Copy; fn len(fat: usize) -> usize; fn empty(len: usize) -> (usize, Self::Field); fn store(len: usize, capacity: usize) -> (usize, Self::Field); fn unpack(fat: usize, capacity: Self::NonZero) -> (usize, usize); fn maybe(fat: usize, capacity: Self::Field) -> Option; } /// Helper trait required by `Cow` to extract capacity of owned /// variant of `T`, and manage conversions. /// /// This can be only implemented on types that match requirements: /// /// + `T::Owned` has a `capacity`, which is an extra word that is absent in `T`. /// + `T::Owned` with `capacity` of `0` does not allocate memory. /// + `T::Owned` can be reconstructed from `*mut T` borrowed out of it, plus capacity. pub unsafe trait Beef: ToOwned { type PointerT; fn ref_into_parts(&self) -> (NonNull, usize, U::Field) where U: Capacity; unsafe fn ref_from_parts(ptr: NonNull, len: usize) -> *const Self where U: Capacity; /// Convert `T::Owned` to `NonNull` and capacity. /// Return `None` for `0` capacity. fn owned_into_parts(owned: Self::Owned) -> (NonNull, usize, U::Field) where U: Capacity; /// Rebuild `T::Owned` from `NonNull` and `capacity`. This can be done by the likes /// of [`Vec::from_raw_parts`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts). unsafe fn owned_from_parts( ptr: NonNull, fat: usize, capacity: U::NonZero, ) -> Self::Owned where U: Capacity; } unsafe impl Beef for str { type PointerT = u8; #[inline] fn ref_into_parts(&self) -> (NonNull, usize, U::Field) where U: Capacity, { let (fat, cap) = U::empty(self.len()); // A note on soundness: // // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ( unsafe { NonNull::new_unchecked(self.as_ptr() as *mut u8) }, fat, cap, ) } #[inline] unsafe fn ref_from_parts(ptr: NonNull, fat: usize) -> *const str where U: Capacity, { slice_from_raw_parts(ptr.as_ptr(), U::len(fat)) as *const str } #[inline] fn owned_into_parts(owned: String) -> (NonNull, usize, U::Field) where U: Capacity, { // Convert to `String::into_raw_parts` once stabilized // We need to go through Vec here to get provenance for the entire allocation // instead of just the initialized parts. let mut owned = ManuallyDrop::new(owned.into_bytes()); let (fat, cap) = U::store(owned.len(), owned.capacity()); ( unsafe { NonNull::new_unchecked(owned.as_mut_ptr()) }, fat, cap, ) } #[inline] unsafe fn owned_from_parts(ptr: NonNull, fat: usize, capacity: U::NonZero) -> String where U: Capacity, { let (len, cap) = U::unpack(fat, capacity); String::from_utf8_unchecked(Vec::from_raw_parts(ptr.as_ptr(), len, cap)) } } unsafe impl Beef for [T] { type PointerT = T; #[inline] fn ref_into_parts(&self) -> (NonNull, usize, U::Field) where U: Capacity, { let (fat, cap) = U::empty(self.len()); // A note on soundness: // // We are casting *const T to *mut T, however for all borrowed values // this raw pointer is only ever dereferenced back to &T. ( unsafe { NonNull::new_unchecked(self.as_ptr() as *mut T) }, fat, cap, ) } #[inline] unsafe fn ref_from_parts(ptr: NonNull, fat: usize) -> *const [T] where U: Capacity, { slice_from_raw_parts(ptr.as_ptr(), U::len(fat)) } #[inline] fn owned_into_parts(owned: Vec) -> (NonNull, usize, U::Field) where U: Capacity, { // Convert to `Vec::into_raw_parts` once stabilized let mut owned = ManuallyDrop::new(owned); let (fat, cap) = U::store(owned.len(), owned.capacity()); ( unsafe { NonNull::new_unchecked(owned.as_mut_ptr()) }, fat, cap, ) } #[inline] unsafe fn owned_from_parts(ptr: NonNull, fat: usize, capacity: U::NonZero) -> Vec where U: Capacity, { let (len, cap) = U::unpack(fat, capacity); Vec::from_raw_parts(ptr.as_ptr(), len, cap) } } } beef-0.5.2/src/wide.rs000064400000000000000000000020030072674642500126410ustar 00000000000000use crate::traits::Capacity; use core::num::NonZeroUsize; /// Compact three word `Cow` that puts the ownership tag in capacity. /// This is a type alias, for documentation see [`beef::generic::Cow`](./generic/struct.Cow.html). pub type Cow<'a, T> = crate::generic::Cow<'a, T, Wide>; pub(crate) mod internal { #[derive(Clone, Copy, PartialEq, Eq)] pub struct Wide; } use internal::Wide; impl Capacity for Wide { type Field = Option; type NonZero = NonZeroUsize; #[inline] fn len(fat: usize) -> usize { fat } #[inline] fn empty(len: usize) -> (usize, Self::Field) { (len, None) } #[inline] fn store(len: usize, capacity: usize) -> (usize, Self::Field) { (len, NonZeroUsize::new(capacity)) } #[inline] fn unpack(fat: usize, capacity: NonZeroUsize) -> (usize, usize) { (fat, capacity.get()) } #[inline] fn maybe(_: usize, capacity: Option) -> Option { capacity } }