├── .github ├── .well-known │ └── funding-manifest-urls ├── FUNDING.yml └── workflows │ ├── ci.yml │ └── coverage.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── rustfmt.toml ├── src ├── dynamic.rs ├── lib.rs ├── string.rs └── vec.rs └── tests ├── loom.rs ├── tests.rs └── ub_guards.rs /.github/.well-known/funding-manifest-urls: -------------------------------------------------------------------------------- 1 | https://typst.app/funding.json 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [typst] 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | # Run weekly to keep Rust toolchain changes fresh 8 | - cron: '0 0 * * 1' 9 | 10 | jobs: 11 | multiple_toolchains: 12 | name: Stable and Beta tasks 13 | runs-on: ubuntu-latest 14 | strategy: 15 | matrix: 16 | rust: 17 | - 1.73 18 | - beta 19 | 20 | steps: 21 | - uses: actions/checkout@v3 22 | - name: Install ${{ matrix.rust }} toolchain 23 | uses: dtolnay/rust-toolchain@master 24 | with: 25 | toolchain: ${{ matrix.rust }} 26 | components: rustfmt 27 | - name: Check formatting 28 | run: cargo fmt --all --check 29 | - name: Check check 30 | env: 31 | RUSTFLAGS: -D warnings 32 | run: cargo check 33 | - name: Run test suite 34 | run: cargo test 35 | - name: Check docs 36 | env: 37 | RUSTDOCFLAGS: -D warnings 38 | run: cargo doc --no-deps --document-private-items 39 | - name: Run loom tests 40 | env: 41 | RUSTFLAGS: --cfg loom 42 | run: cargo test --release loom --features loom 43 | 44 | # We use a healthy amount of unsafe, so run tests with Miri to check for UB 45 | nightly_only: 46 | name: Nightly tasks 47 | runs-on: ubuntu-latest 48 | 49 | steps: 50 | - uses: actions/checkout@v3 51 | - name: Install nightly toolchain 52 | uses: dtolnay/rust-toolchain@master 53 | with: 54 | toolchain: nightly 55 | components: miri 56 | - name: Miri 64-bit LE 57 | run: cargo miri test --target x86_64-unknown-linux-gnu 58 | - name: Miri 64-bit BE 59 | run: cargo miri test --target sparc64-unknown-linux-gnu 60 | - name: Miri 32-bit LE 61 | run: cargo miri test --target i686-unknown-linux-gnu 62 | -------------------------------------------------------------------------------- /.github/workflows/coverage.yml: -------------------------------------------------------------------------------- 1 | name: Coverage 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | coverage: 7 | runs-on: ubuntu-latest 8 | env: 9 | CARGO_TERM_COLOR: always 10 | steps: 11 | - uses: actions/checkout@v3 12 | - name: Install stable toolchain 13 | uses: dtolnay/rust-toolchain@master 14 | with: 15 | toolchain: stable 16 | - name: Install cargo-llvm-cov 17 | uses: taiki-e/install-action@cargo-llvm-cov 18 | - name: Generate code coverage 19 | run: cargo llvm-cov --lcov --output-path lcov.info 20 | - name: Upload coverage to Codecov 21 | uses: codecov/codecov-action@v3 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ecow" 3 | version = "0.2.5" 4 | rust-version = "1.73" # also change in ci.yml 5 | authors = ["Laurenz "] 6 | edition = "2021" 7 | description = "Compact, clone-on-write vector and string." 8 | repository = "https://github.com/typst/ecow" 9 | readme = "README.md" 10 | license = "MIT OR Apache-2.0" 11 | categories = ["data-structures", "no-std"] 12 | keywords = ["string", "vector", "sso", "cow"] 13 | 14 | [features] 15 | default = ["std"] 16 | std = [] 17 | 18 | [dependencies] 19 | serde = { version = "1.0", optional = true, default-features = false } 20 | 21 | [target.'cfg(loom)'.dependencies] 22 | loom = { version = "0.7", optional = true } 23 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ecow 2 | [![Crates.io](https://img.shields.io/crates/v/ecow.svg)](https://crates.io/crates/ecow) 3 | [![Documentation](https://docs.rs/ecow/badge.svg)](https://docs.rs/ecow) 4 | 5 | Compact, clone-on-write vector and string. 6 | 7 | ## Types 8 | - An `EcoVec` is a reference-counted clone-on-write vector. It takes up two 9 | words of space (= 2 usize) and has the same memory layout as a `&[T]` slice. 10 | Within its allocation, it stores a reference count, its capacity and its 11 | elements. 12 | 13 | - An `EcoString` is a reference-counted clone-on-write string with inline 14 | storage. It takes up 16 bytes of space. It has 15 bytes of inline storage and 15 | starting from 16 bytes it becomes an `EcoVec`. 16 | 17 | ## Example 18 | ```rust 19 | // This is stored inline. 20 | let small = ecow::EcoString::from("Welcome"); 21 | 22 | // This spills to the heap, but only once: `big` and `third` share the 23 | // same underlying allocation. Vectors and spilled strings are only 24 | // really cloned upon mutation. 25 | let big = small + " to earth! 🌱"; 26 | let mut third = big.clone(); 27 | 28 | // This allocates again to mutate `third` without affecting `big`. 29 | assert_eq!(third.pop(), Some('🌱')); 30 | assert_eq!(third, "Welcome to earth! "); 31 | ``` 32 | 33 | ## Why should I use this instead of ... 34 | 35 | | Type | Details | 36 | |:--------------------------------------------|:--------| 37 | | [`Vec`][vec] / [`String`][string] | Normal vectors are a great general purpose data structure. But they have a quite big footprint (3 machine words) and are expensive to clone. The `EcoVec` has a bit of overhead for mutation, but is cheap to clone and only takes two words. | 38 | | [`Arc>`][arc] / [`Arc`][arc] | These require two allocations instead of one and are less convenient to mutate. | 39 | | [`Arc<[T]>`][arc] / [`Arc`][arc] | While these require only one allocation, they aren't mutable. | 40 | | Small vector | Different trade-off. Great when there are few, small `T`s, but expensive to clone when spilled to the heap. | 41 | | Small string | The `EcoString` combines different small string qualities into a very practical package: It has inline storage, a smaller footprint than a normal `String`, is efficient to clone even when spilled, and at the same time mutable. | 42 | 43 | [vec]: https://doc.rust-lang.org/std/vec/struct.Vec.html 44 | [string]: https://doc.rust-lang.org/std/string/struct.String.html 45 | [arc]: https://doc.rust-lang.org/std/sync/struct.Arc.html 46 | 47 | ## License 48 | This crate is dual-licensed under the MIT and Apache 2.0 licenses. 49 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | use_small_heuristics = "Max" 2 | max_width = 90 3 | chain_width = 70 4 | struct_lit_width = 50 5 | use_field_init_shorthand = true 6 | merge_derives = false 7 | -------------------------------------------------------------------------------- /src/dynamic.rs: -------------------------------------------------------------------------------- 1 | use core::mem::{self, ManuallyDrop}; 2 | use core::ptr; 3 | 4 | use super::EcoVec; 5 | 6 | /// A byte vector that can hold up to 15 bytes inline and then spills to an 7 | /// `EcoVec`. 8 | pub(crate) struct DynamicVec(Repr); 9 | 10 | /// The internal representation. 11 | /// 12 | /// On 64-bit little endian, this assumes that no valid EcoVec exists in which 13 | /// the highest-order bit of the InlineVec's `tagged_len` would be set. This is 14 | /// true because EcoVec is repr(C) and its second field `len` is bounded by 15 | /// `isize::MAX`. On 32-bit, it's no problem and for 64-bit big endian, we 16 | /// have an increased limit to prevent the overlap. 17 | #[repr(C)] 18 | union Repr { 19 | inline: InlineVec, 20 | spilled: ManuallyDrop>, 21 | } 22 | 23 | /// This is never stored in memory, it's just an abstraction for safe access. 24 | #[derive(Debug)] 25 | enum Variant<'a> { 26 | Inline(&'a InlineVec), 27 | Spilled(&'a EcoVec), 28 | } 29 | 30 | /// This is never stored in memory, it's just an abstraction for safe access. 31 | #[derive(Debug)] 32 | enum VariantMut<'a> { 33 | Inline(&'a mut InlineVec), 34 | Spilled(&'a mut EcoVec), 35 | } 36 | 37 | /// The maximum amount of inline storage. Typically, this is 15 bytes. 38 | /// 39 | /// However, in the rare exotic system, we still want things to be safe. 40 | /// Therefore, the following special cases: 41 | /// - For big endian, we increase the limit such that the tagged length of the 42 | /// inline variants doesn't overlap with the EcoVec. For little endian, it's 43 | /// fine since the highest order bit is never set for a valid EcoVec. 44 | /// - In case somehow EcoVec is very big (128-bit pointers woah), increase the 45 | /// limit too. 46 | pub(crate) const LIMIT: usize = { 47 | let mut limit = 15; 48 | if limit < mem::size_of::>() - 1 { 49 | limit = mem::size_of::>() - 1; 50 | } 51 | if cfg!(target_endian = "big") { 52 | limit += mem::size_of::(); 53 | } 54 | limit 55 | }; 56 | 57 | /// This bit is used to check whether we are inline or not. On 64-bit little 58 | /// endian, it coincides with the highest-order bit of an EcoVec's length, which 59 | /// can't be set because the EcoVec's length never exceeds `isize::MAX`. 60 | const LEN_TAG: u8 = 0b1000_0000; 61 | 62 | /// This is used to mask off the tag to get the inline variant's length. 63 | const LEN_MASK: u8 = 0b0111_1111; 64 | 65 | impl DynamicVec { 66 | #[inline] 67 | pub const fn new() -> Self { 68 | Self::from_inline(InlineVec::new()) 69 | } 70 | 71 | #[inline] 72 | pub const fn from_inline(inline: InlineVec) -> Self { 73 | Self(Repr { inline }) 74 | } 75 | 76 | #[inline] 77 | pub const fn from_eco(vec: EcoVec) -> Self { 78 | // Safety: 79 | // Explicitly set `tagged_len` to 0 to mark this as a spilled variant. 80 | // Just initializing with `Repr { spilled: ... }` would leave 81 | // `tagged_len` uninitialized, leading to undefined behaviour on access. 82 | let mut repr = Repr { 83 | inline: InlineVec { buf: [0; LIMIT], tagged_len: 0 }, 84 | }; 85 | repr.spilled = ManuallyDrop::new(vec); 86 | Self(repr) 87 | } 88 | 89 | #[inline] 90 | pub fn from_slice(bytes: &[u8]) -> Self { 91 | match InlineVec::from_slice(bytes) { 92 | Ok(inline) => Self::from_inline(inline), 93 | _ => Self::from_eco(EcoVec::from(bytes)), 94 | } 95 | } 96 | 97 | #[inline] 98 | pub fn with_capacity(capacity: usize) -> Self { 99 | if capacity <= LIMIT { 100 | Self::new() 101 | } else { 102 | Self::from_eco(EcoVec::with_capacity(capacity)) 103 | } 104 | } 105 | 106 | #[inline] 107 | pub fn len(&self) -> usize { 108 | match self.variant() { 109 | Variant::Inline(inline) => inline.len(), 110 | Variant::Spilled(spilled) => spilled.len(), 111 | } 112 | } 113 | 114 | #[inline] 115 | pub fn as_slice(&self) -> &[u8] { 116 | match self.variant() { 117 | Variant::Inline(inline) => inline.as_slice(), 118 | Variant::Spilled(spilled) => spilled.as_slice(), 119 | } 120 | } 121 | 122 | #[inline] 123 | pub fn make_mut(&mut self) -> &mut [u8] { 124 | match self.variant_mut() { 125 | VariantMut::Inline(inline) => inline.as_mut_slice(), 126 | VariantMut::Spilled(spilled) => spilled.make_mut(), 127 | } 128 | } 129 | 130 | #[inline] 131 | pub fn push(&mut self, byte: u8) { 132 | match self.variant_mut() { 133 | VariantMut::Inline(inline) => { 134 | if inline.push(byte).is_err() { 135 | let mut eco = EcoVec::with_capacity(LIMIT * 2); 136 | eco.extend_from_byte_slice(self.as_slice()); 137 | eco.push(byte); 138 | *self = Self::from_eco(eco); 139 | } 140 | } 141 | VariantMut::Spilled(spilled) => { 142 | spilled.push(byte); 143 | } 144 | } 145 | } 146 | 147 | #[inline] 148 | pub fn extend_from_slice(&mut self, bytes: &[u8]) { 149 | match self.variant_mut() { 150 | VariantMut::Inline(inline) => { 151 | if inline.extend_from_slice(bytes).is_err() { 152 | let needed = inline.len() + bytes.len(); 153 | let mut eco = EcoVec::with_capacity(needed.next_power_of_two()); 154 | eco.extend_from_byte_slice(self.as_slice()); 155 | eco.extend_from_byte_slice(bytes); 156 | *self = Self::from_eco(eco); 157 | } 158 | } 159 | VariantMut::Spilled(spilled) => { 160 | spilled.extend_from_byte_slice(bytes); 161 | } 162 | } 163 | } 164 | 165 | #[inline] 166 | pub fn clear(&mut self) { 167 | match self.variant_mut() { 168 | VariantMut::Inline(inline) => inline.clear(), 169 | VariantMut::Spilled(spilled) => spilled.clear(), 170 | } 171 | } 172 | 173 | #[inline] 174 | pub fn truncate(&mut self, target: usize) { 175 | match self.variant_mut() { 176 | VariantMut::Inline(inline) => inline.truncate(target), 177 | VariantMut::Spilled(spilled) => spilled.truncate(target), 178 | } 179 | } 180 | } 181 | 182 | impl DynamicVec { 183 | // If this returns true, guarantees that `self.0.inline` is initialized. 184 | // Otherwise, guarantees that `self.0.spilled` is initialized. 185 | #[inline] 186 | fn is_inline(&self) -> bool { 187 | // Safety: 188 | // We always initialize tagged_len, even for the `EcoVec` variant. For 189 | // the inline variant the highest-order bit is always `1`. For the 190 | // spilled variant, it is initialized with `0` and cannot deviate from 191 | // that because the EcoVec's `len` field is bounded by `isize::MAX`. (At 192 | // least on 64-bit little endian; on 32-bit or big-endian the EcoVec 193 | // and tagged_len fields don't even overlap, meaning tagged_len stays at 194 | // its initial value.) 195 | unsafe { self.0.inline.tagged_len & LEN_TAG != 0 } 196 | } 197 | 198 | #[inline] 199 | fn variant(&self) -> Variant<'_> { 200 | unsafe { 201 | // Safety: 202 | // We access the respective variant only if the check passes. 203 | if self.is_inline() { 204 | Variant::Inline(&self.0.inline) 205 | } else { 206 | Variant::Spilled(&self.0.spilled) 207 | } 208 | } 209 | } 210 | 211 | #[inline] 212 | fn variant_mut(&mut self) -> VariantMut<'_> { 213 | unsafe { 214 | // Safety: 215 | // We access the respective variant only if the check passes. 216 | if self.is_inline() { 217 | VariantMut::Inline(&mut self.0.inline) 218 | } else { 219 | VariantMut::Spilled(&mut self.0.spilled) 220 | } 221 | } 222 | } 223 | } 224 | 225 | impl Clone for DynamicVec { 226 | #[inline] 227 | fn clone(&self) -> Self { 228 | match self.variant() { 229 | Variant::Inline(inline) => Self::from_inline(*inline), 230 | Variant::Spilled(spilled) => Self::from_eco(spilled.clone()), 231 | } 232 | } 233 | } 234 | 235 | impl Drop for DynamicVec { 236 | #[inline] 237 | fn drop(&mut self) { 238 | if let VariantMut::Spilled(spilled) = self.variant_mut() { 239 | unsafe { 240 | // Safety: We are guaranteed to have a valid `EcoVec`. 241 | ptr::drop_in_place(spilled); 242 | } 243 | } 244 | } 245 | } 246 | 247 | #[repr(C)] 248 | #[derive(Debug, Copy, Clone)] 249 | pub(crate) struct InlineVec { 250 | /// Storage! 251 | buf: [u8; LIMIT], 252 | /// Invariant: After masking off the tag, never exceeds LIMIT. 253 | tagged_len: u8, 254 | } 255 | 256 | impl InlineVec { 257 | #[inline] 258 | pub const fn new() -> Self { 259 | // Safety: Trivially, 0 <= LIMIT 260 | unsafe { Self::from_buf([0; LIMIT], 0) } 261 | } 262 | 263 | #[inline] 264 | pub const fn from_slice(bytes: &[u8]) -> Result { 265 | let len = bytes.len(); 266 | if len > LIMIT { 267 | return Err(()); 268 | } 269 | 270 | let mut buf = [0; LIMIT]; 271 | let mut i = 0; 272 | while i < len { 273 | buf[i] = bytes[i]; 274 | i += 1; 275 | } 276 | 277 | // Safety: If len > LIMIT, Err was returned earlier. 278 | unsafe { Ok(Self::from_buf(buf, len)) } 279 | } 280 | 281 | /// The given length may not exceed LIMIT. 282 | #[inline] 283 | pub const unsafe fn from_buf(buf: [u8; LIMIT], len: usize) -> Self { 284 | debug_assert!(len <= LIMIT); 285 | Self { buf, tagged_len: len as u8 | LEN_TAG } 286 | } 287 | 288 | #[inline] 289 | pub fn len(&self) -> usize { 290 | usize::from(self.tagged_len & LEN_MASK) 291 | } 292 | 293 | /// The given length may not exceed LIMIT. 294 | #[inline] 295 | unsafe fn set_len(&mut self, len: usize) { 296 | debug_assert!(len <= LIMIT); 297 | self.tagged_len = len as u8 | LEN_TAG; 298 | } 299 | 300 | #[inline] 301 | pub fn as_slice(&self) -> &[u8] { 302 | // Safety: We have the invariant `len <= LIMIT`. 303 | unsafe { self.buf.get_unchecked(..self.len()) } 304 | } 305 | 306 | #[inline] 307 | pub fn as_mut_slice(&mut self) -> &mut [u8] { 308 | // Safety: We have the invariant `len <= LIMIT`. 309 | let len = self.len(); 310 | unsafe { self.buf.get_unchecked_mut(..len) } 311 | } 312 | 313 | #[inline] 314 | pub fn clear(&mut self) { 315 | unsafe { 316 | // Safety: Trivially, `0 <= LIMIT`. 317 | self.set_len(0); 318 | } 319 | } 320 | 321 | #[inline] 322 | pub fn push(&mut self, byte: u8) -> Result<(), ()> { 323 | let len = self.len(); 324 | if let Some(slot) = self.buf.get_mut(len) { 325 | *slot = byte; 326 | unsafe { 327 | // Safety: The `get_mut` call guarantees that `len < LIMIT`. 328 | self.set_len(len + 1); 329 | } 330 | Ok(()) 331 | } else { 332 | Err(()) 333 | } 334 | } 335 | 336 | #[inline] 337 | pub fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), ()> { 338 | let len = self.len(); 339 | let grown = len + bytes.len(); 340 | if let Some(segment) = self.buf.get_mut(len..grown) { 341 | segment.copy_from_slice(bytes); 342 | unsafe { 343 | // Safety: The `get_mut` call guarantees that `grown <= LIMIT`. 344 | self.set_len(grown); 345 | } 346 | Ok(()) 347 | } else { 348 | Err(()) 349 | } 350 | } 351 | 352 | #[inline] 353 | pub fn truncate(&mut self, target: usize) { 354 | if target < self.len() { 355 | unsafe { 356 | // Safety: Checked that it's smaller than the current length, 357 | // which cannot exceed LIMIT itself. 358 | self.set_len(target); 359 | } 360 | } 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /*! 2 | Compact, clone-on-write vector and string. 3 | 4 | ## Types 5 | - An [`EcoVec`] is a reference-counted clone-on-write vector. It takes up two 6 | words of space (= 2 usize) and has the same memory layout as a `&[T]` slice. 7 | Within its allocation, it stores a reference count, its capacity and its 8 | elements. 9 | 10 | - An [`EcoString`] is a reference-counted clone-on-write string with inline 11 | storage. It takes up 16 bytes of space. It has 15 bytes of inline storage and 12 | starting from 16 bytes it becomes an [`EcoVec`]. 13 | 14 | ## Example 15 | ``` 16 | // This is stored inline. 17 | let small = ecow::EcoString::from("Welcome"); 18 | 19 | // This spills to the heap, but only once: `big` and `third` share the 20 | // same underlying allocation. Vectors and spilled strings are only 21 | // really cloned upon mutation. 22 | let big = small + " to earth! 🌱"; 23 | let mut third = big.clone(); 24 | 25 | // This allocates again to mutate `third` without affecting `big`. 26 | assert_eq!(third.pop(), Some('🌱')); 27 | assert_eq!(third, "Welcome to earth! "); 28 | ``` 29 | 30 | ## Why should I use this instead of ... 31 | 32 | | Type | Details | 33 | |:----------------------------------|:--------| 34 | | [`Vec`]/ [`String`] | Normal vectors are a great general purpose data structure. But they have a quite big footprint (3 machine words) and are expensive to clone. The [`EcoVec`] has a bit of overhead for mutation, but is cheap to clone and only takes two words. | 35 | | [`Arc>`] / [`Arc`] | These require two allocations instead of one and are less convenient to mutate. | 36 | | [`Arc<[T]>`] / [`Arc`] | While these require only one allocation, they aren't mutable. | 37 | | Small vector | Different trade-off. Great when there are few, small `T`s, but expensive to clone when spilled to the heap. | 38 | | Small string | The [`EcoString`] combines different small string qualities into a very practical package: It has inline storage, a smaller footprint than a normal [`String`][string], is efficient to clone even when spilled, and at the same time mutable. | 39 | */ 40 | 41 | #![cfg_attr(not(feature = "std"), no_std)] 42 | #![deny(missing_docs)] 43 | // See https://github.com/tokio-rs/loom/issues/352 44 | #![allow(unknown_lints, unexpected_cfgs)] 45 | 46 | extern crate alloc; 47 | 48 | pub mod string; 49 | pub mod vec; 50 | 51 | mod dynamic; 52 | 53 | pub use self::string::EcoString; 54 | pub use self::vec::EcoVec; 55 | 56 | #[cfg(doc)] 57 | use alloc::{string::String, sync::Arc, vec::Vec}; 58 | 59 | // Run doctests on the README too 60 | #[doc = include_str!("../README.md")] 61 | #[cfg(doctest)] 62 | pub struct ReadmeDoctests; 63 | 64 | /// Loom needs its own synchronization types to be used in order to work 65 | mod sync { 66 | /// Atomics stub 67 | pub mod atomic { 68 | #[cfg(not(loom))] 69 | pub use core::sync::atomic::*; 70 | 71 | #[cfg(loom)] 72 | pub use loom::sync::atomic::*; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/string.rs: -------------------------------------------------------------------------------- 1 | //! A clone-on-write, small-string-optimized alternative to [`String`]. 2 | 3 | use alloc::borrow::Cow; 4 | use core::borrow::Borrow; 5 | use core::cmp::Ordering; 6 | use core::fmt::{self, Debug, Display, Formatter, Write}; 7 | use core::hash::{Hash, Hasher}; 8 | use core::ops::{Add, AddAssign, Deref}; 9 | use core::str::FromStr; 10 | 11 | #[cfg(not(feature = "std"))] 12 | use alloc::string::String; 13 | 14 | use crate::dynamic::{DynamicVec, InlineVec}; 15 | 16 | /// Create a new [`EcoString`] from a format string. 17 | /// ``` 18 | /// # use ecow::eco_format; 19 | /// assert_eq!(eco_format!("Hello, {}!", 123), "Hello, 123!"); 20 | /// ``` 21 | #[macro_export] 22 | macro_rules! eco_format { 23 | ($($tts:tt)*) => {{ 24 | use ::std::fmt::Write; 25 | let mut s = $crate::EcoString::new(); 26 | ::std::write!(s, $($tts)*).unwrap(); 27 | s 28 | }}; 29 | } 30 | 31 | /// An economical string with inline storage and clone-on-write semantics. 32 | /// 33 | /// This type has a size of 16 bytes. It has 15 bytes of inline storage and 34 | /// starting from 16 bytes it becomes an [`EcoVec`](super::EcoVec). The 35 | /// internal reference counter of the heap variant is atomic, making this type 36 | /// [`Sync`] and [`Send`]. 37 | /// 38 | /// # Example 39 | /// ``` 40 | /// use ecow::EcoString; 41 | /// 42 | /// // This is stored inline. 43 | /// let small = EcoString::from("Welcome"); 44 | /// 45 | /// // This spills to the heap only once: `big` and `third` share the same 46 | /// // underlying allocation. Just like vectors, heap strings are only really 47 | /// // cloned upon mutation. 48 | /// let big = small + " to earth! 🌱"; 49 | /// let mut third = big.clone(); 50 | /// assert_eq!(big, "Welcome to earth! 🌱"); 51 | /// assert_eq!(third, big); 52 | /// 53 | /// // This allocates again to mutate `third` without affecting `big`. 54 | /// assert_eq!(third.pop(), Some('🌱')); 55 | /// assert_eq!(third, "Welcome to earth! "); 56 | /// assert_eq!(big, "Welcome to earth! 🌱"); 57 | /// ``` 58 | /// 59 | /// # Note 60 | /// The above holds true for normal 32-bit or 64-bit little endian systems. On 61 | /// 64-bit big-endian systems, the type's size increases to 24 bytes and the 62 | /// amount of inline storage to 23 bytes. 63 | #[derive(Clone)] 64 | pub struct EcoString(DynamicVec); 65 | 66 | impl EcoString { 67 | /// Maximum number of bytes for an inline `EcoString` before spilling on 68 | /// the heap. 69 | /// 70 | /// The exact value for this is architecture dependent. 71 | /// 72 | /// # Note 73 | /// This value is semver exempt and can be changed with any update. 74 | pub const INLINE_LIMIT: usize = crate::dynamic::LIMIT; 75 | 76 | /// Create a new, empty string. 77 | #[inline] 78 | pub const fn new() -> Self { 79 | Self(DynamicVec::new()) 80 | } 81 | 82 | /// Create a new, inline string. 83 | /// 84 | /// Panics if the string's length exceeds the capacity of the inline 85 | /// storage. 86 | #[inline] 87 | pub const fn inline(string: &str) -> Self { 88 | let Ok(inline) = InlineVec::from_slice(string.as_bytes()) else { 89 | exceeded_inline_capacity(); 90 | }; 91 | Self(DynamicVec::from_inline(inline)) 92 | } 93 | 94 | /// Create a new, empty string with the given `capacity`. 95 | #[inline] 96 | pub fn with_capacity(capacity: usize) -> Self { 97 | Self(DynamicVec::with_capacity(capacity)) 98 | } 99 | 100 | /// Create an instance from a string slice. 101 | #[inline] 102 | fn from_str(string: &str) -> Self { 103 | Self(DynamicVec::from_slice(string.as_bytes())) 104 | } 105 | 106 | /// Whether the string is empty. 107 | #[inline] 108 | pub fn is_empty(&self) -> bool { 109 | self.len() == 0 110 | } 111 | 112 | /// The length of the string in bytes. 113 | #[inline] 114 | pub fn len(&self) -> usize { 115 | self.0.len() 116 | } 117 | 118 | /// A string slice containing the entire string. 119 | #[inline] 120 | pub fn as_str(&self) -> &str { 121 | // Safety: 122 | // The buffer contents stem from correct UTF-8 sources: 123 | // - Valid ASCII characters 124 | // - Other string slices 125 | // - Chars that were encoded with char::encode_utf8 126 | unsafe { core::str::from_utf8_unchecked(self.0.as_slice()) } 127 | } 128 | 129 | /// Produce a mutable slice containing the entire string. 130 | /// 131 | /// Clones the string if its reference count is larger than 1. 132 | #[inline] 133 | pub fn make_mut(&mut self) -> &mut str { 134 | // Safety: 135 | // The buffer contents stem from correct UTF-8 sources: 136 | // - Valid ASCII characters 137 | // - Other string slices 138 | // - Chars that were encoded with char::encode_utf8 139 | unsafe { core::str::from_utf8_unchecked_mut(self.0.make_mut()) } 140 | } 141 | 142 | /// Append the given character at the end. 143 | #[inline] 144 | pub fn push(&mut self, c: char) { 145 | if c.len_utf8() == 1 { 146 | self.0.push(c as u8); 147 | } else { 148 | self.push_str(c.encode_utf8(&mut [0; 4])); 149 | } 150 | } 151 | 152 | /// Append the given string slice at the end. 153 | pub fn push_str(&mut self, string: &str) { 154 | self.0.extend_from_slice(string.as_bytes()); 155 | } 156 | 157 | /// Remove the last character from the string. 158 | #[inline] 159 | pub fn pop(&mut self) -> Option { 160 | let slice = self.as_str(); 161 | let c = slice.chars().next_back()?; 162 | self.0.truncate(slice.len() - c.len_utf8()); 163 | Some(c) 164 | } 165 | 166 | /// Clear the string. 167 | #[inline] 168 | pub fn clear(&mut self) { 169 | self.0.clear(); 170 | } 171 | 172 | /// Shortens the string to the specified length. 173 | /// 174 | /// If `new_len` is greater than or equal to the string's current length, 175 | /// this has no effect. 176 | /// 177 | /// Panics if `new_len` does not lie on a [`char`] boundary. 178 | #[inline] 179 | pub fn truncate(&mut self, new_len: usize) { 180 | if new_len <= self.len() { 181 | assert!(self.is_char_boundary(new_len)); 182 | self.0.truncate(new_len); 183 | } 184 | } 185 | 186 | /// Replaces all matches of a string with another string. 187 | /// 188 | /// This is a bit less general that [`str::replace`] because the `Pattern` 189 | /// trait is unstable. In return, it can produce an `EcoString` without 190 | /// any intermediate [`String`] allocation. 191 | pub fn replace(&self, pat: &str, to: &str) -> Self { 192 | self.replacen(pat, to, usize::MAX) 193 | } 194 | 195 | /// Replaces the first N matches of a string with another string. 196 | /// 197 | /// This is a bit less general that [`str::replacen`] because the `Pattern` 198 | /// trait is unstable. In return, it can produce an `EcoString` without 199 | /// any intermediate [`String`] allocation. 200 | pub fn replacen(&self, pat: &str, to: &str, count: usize) -> Self { 201 | // Copied from the standard library: https://github.com/rust-lang/rust 202 | let mut result = Self::new(); 203 | let mut last_end = 0; 204 | for (start, part) in self.match_indices(pat).take(count) { 205 | // Safety: Copied from std. 206 | result.push_str(unsafe { self.get_unchecked(last_end..start) }); 207 | result.push_str(to); 208 | last_end = start + part.len(); 209 | } 210 | // Safety: Copied from std. 211 | result.push_str(unsafe { self.get_unchecked(last_end..self.len()) }); 212 | result 213 | } 214 | 215 | /// Returns the lowercase equivalent of this string. 216 | pub fn to_lowercase(&self) -> Self { 217 | let str = self.as_str(); 218 | let mut lower = Self::with_capacity(str.len()); 219 | for c in str.chars() { 220 | // Let std handle the special case. 221 | if c == 'Σ' { 222 | return str.to_lowercase().into(); 223 | } 224 | for v in c.to_lowercase() { 225 | lower.push(v); 226 | } 227 | } 228 | lower 229 | } 230 | 231 | /// Returns the uppercase equivalent of this string. 232 | pub fn to_uppercase(&self) -> Self { 233 | let str = self.as_str(); 234 | let mut upper = Self::with_capacity(str.len()); 235 | for c in str.chars() { 236 | for v in c.to_uppercase() { 237 | upper.push(v); 238 | } 239 | } 240 | upper 241 | } 242 | 243 | /// Returns a copy of this string where each character is mapped to its 244 | /// ASCII uppercase equivalent. 245 | pub fn to_ascii_lowercase(&self) -> Self { 246 | let mut s = self.clone(); 247 | s.make_mut().make_ascii_lowercase(); 248 | s 249 | } 250 | 251 | /// Returns a copy of this string where each character is mapped to its 252 | /// ASCII uppercase equivalent. 253 | pub fn to_ascii_uppercase(&self) -> Self { 254 | let mut s = self.clone(); 255 | s.make_mut().make_ascii_uppercase(); 256 | s 257 | } 258 | 259 | /// Repeat this string `n` times. 260 | pub fn repeat(&self, n: usize) -> Self { 261 | let slice = self.as_bytes(); 262 | let capacity = slice.len().saturating_mul(n); 263 | let mut vec = DynamicVec::with_capacity(capacity); 264 | for _ in 0..n { 265 | vec.extend_from_slice(slice); 266 | } 267 | Self(vec) 268 | } 269 | } 270 | 271 | impl Deref for EcoString { 272 | type Target = str; 273 | 274 | #[inline] 275 | fn deref(&self) -> &str { 276 | self.as_str() 277 | } 278 | } 279 | 280 | impl Default for EcoString { 281 | #[inline] 282 | fn default() -> Self { 283 | Self::new() 284 | } 285 | } 286 | 287 | impl Debug for EcoString { 288 | #[inline] 289 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { 290 | Debug::fmt(self.as_str(), f) 291 | } 292 | } 293 | 294 | impl Display for EcoString { 295 | #[inline] 296 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { 297 | Display::fmt(self.as_str(), f) 298 | } 299 | } 300 | 301 | impl Eq for EcoString {} 302 | 303 | impl PartialEq for EcoString { 304 | #[inline] 305 | fn eq(&self, other: &Self) -> bool { 306 | self.as_str().eq(other.as_str()) 307 | } 308 | } 309 | 310 | impl PartialEq for EcoString { 311 | #[inline] 312 | fn eq(&self, other: &str) -> bool { 313 | self.as_str().eq(other) 314 | } 315 | } 316 | 317 | impl PartialEq<&str> for EcoString { 318 | #[inline] 319 | fn eq(&self, other: &&str) -> bool { 320 | self.as_str().eq(*other) 321 | } 322 | } 323 | 324 | impl PartialEq for EcoString { 325 | #[inline] 326 | fn eq(&self, other: &String) -> bool { 327 | self.as_str().eq(other) 328 | } 329 | } 330 | 331 | impl PartialEq for str { 332 | #[inline] 333 | fn eq(&self, other: &EcoString) -> bool { 334 | self.eq(other.as_str()) 335 | } 336 | } 337 | 338 | impl PartialEq for &str { 339 | #[inline] 340 | fn eq(&self, other: &EcoString) -> bool { 341 | (*self).eq(other.as_str()) 342 | } 343 | } 344 | 345 | impl PartialEq for String { 346 | #[inline] 347 | fn eq(&self, other: &EcoString) -> bool { 348 | self.eq(other.as_str()) 349 | } 350 | } 351 | 352 | impl Ord for EcoString { 353 | #[inline] 354 | fn cmp(&self, other: &Self) -> Ordering { 355 | self.as_str().cmp(other.as_str()) 356 | } 357 | } 358 | 359 | impl PartialOrd for EcoString { 360 | #[inline] 361 | fn partial_cmp(&self, other: &Self) -> Option { 362 | Some(self.cmp(other)) 363 | } 364 | } 365 | 366 | impl Hash for EcoString { 367 | #[inline] 368 | fn hash(&self, state: &mut H) { 369 | self.as_str().hash(state); 370 | } 371 | } 372 | 373 | impl Write for EcoString { 374 | #[inline] 375 | fn write_str(&mut self, s: &str) -> fmt::Result { 376 | self.push_str(s); 377 | Ok(()) 378 | } 379 | 380 | #[inline] 381 | fn write_char(&mut self, c: char) -> fmt::Result { 382 | self.push(c); 383 | Ok(()) 384 | } 385 | } 386 | 387 | impl Add for EcoString { 388 | type Output = Self; 389 | 390 | #[inline] 391 | fn add(mut self, rhs: Self) -> Self::Output { 392 | self += rhs; 393 | self 394 | } 395 | } 396 | 397 | impl AddAssign for EcoString { 398 | #[inline] 399 | fn add_assign(&mut self, rhs: Self) { 400 | self.push_str(rhs.as_str()); 401 | } 402 | } 403 | 404 | impl Add<&str> for EcoString { 405 | type Output = Self; 406 | 407 | #[inline] 408 | fn add(mut self, rhs: &str) -> Self::Output { 409 | self += rhs; 410 | self 411 | } 412 | } 413 | 414 | impl AddAssign<&str> for EcoString { 415 | #[inline] 416 | fn add_assign(&mut self, rhs: &str) { 417 | self.push_str(rhs); 418 | } 419 | } 420 | 421 | impl AsRef for EcoString { 422 | #[inline] 423 | fn as_ref(&self) -> &str { 424 | self 425 | } 426 | } 427 | 428 | impl Borrow for EcoString { 429 | #[inline] 430 | fn borrow(&self) -> &str { 431 | self 432 | } 433 | } 434 | 435 | impl From for EcoString { 436 | #[inline] 437 | fn from(c: char) -> Self { 438 | Self::inline(c.encode_utf8(&mut [0; 4])) 439 | } 440 | } 441 | 442 | impl From<&str> for EcoString { 443 | #[inline] 444 | fn from(s: &str) -> Self { 445 | Self::from_str(s) 446 | } 447 | } 448 | 449 | impl From for EcoString { 450 | /// When the string does not fit inline, this needs to allocate to change 451 | /// the layout. 452 | #[inline] 453 | fn from(s: String) -> Self { 454 | Self::from_str(&s) 455 | } 456 | } 457 | 458 | impl From<&String> for EcoString { 459 | #[inline] 460 | fn from(s: &String) -> Self { 461 | Self::from_str(s.as_str()) 462 | } 463 | } 464 | 465 | impl From<&EcoString> for EcoString { 466 | #[inline] 467 | fn from(s: &EcoString) -> Self { 468 | s.clone() 469 | } 470 | } 471 | 472 | impl From> for EcoString { 473 | #[inline] 474 | fn from(s: Cow) -> Self { 475 | Self::from_str(&s) 476 | } 477 | } 478 | 479 | impl FromIterator for EcoString { 480 | #[inline] 481 | fn from_iter>(iter: T) -> Self { 482 | let mut s = Self::new(); 483 | for c in iter { 484 | s.push(c); 485 | } 486 | s 487 | } 488 | } 489 | 490 | impl FromIterator for EcoString { 491 | #[inline] 492 | fn from_iter>(iter: T) -> Self { 493 | let mut s = Self::new(); 494 | for piece in iter { 495 | s.push_str(&piece); 496 | } 497 | s 498 | } 499 | } 500 | 501 | impl Extend for EcoString { 502 | #[inline] 503 | fn extend>(&mut self, iter: T) { 504 | for c in iter { 505 | self.push(c); 506 | } 507 | } 508 | } 509 | 510 | impl From for String { 511 | /// This needs to allocate to change the layout. 512 | #[inline] 513 | fn from(s: EcoString) -> Self { 514 | s.as_str().into() 515 | } 516 | } 517 | 518 | impl From<&EcoString> for String { 519 | #[inline] 520 | fn from(s: &EcoString) -> Self { 521 | s.as_str().into() 522 | } 523 | } 524 | 525 | impl FromStr for EcoString { 526 | type Err = core::convert::Infallible; 527 | 528 | #[inline] 529 | fn from_str(s: &str) -> Result { 530 | Ok(Self::from_str(s)) 531 | } 532 | } 533 | 534 | #[cold] 535 | const fn exceeded_inline_capacity() -> ! { 536 | panic!("exceeded inline capacity"); 537 | } 538 | 539 | #[cfg(feature = "serde")] 540 | mod serde { 541 | use crate::EcoString; 542 | use core::fmt; 543 | use serde::de::{Deserializer, Error, Unexpected, Visitor}; 544 | 545 | impl serde::Serialize for EcoString { 546 | fn serialize(&self, serializer: S) -> Result 547 | where 548 | S: serde::Serializer, 549 | { 550 | self.as_str().serialize(serializer) 551 | } 552 | } 553 | 554 | impl<'de> serde::Deserialize<'de> for EcoString { 555 | fn deserialize(deserializer: D) -> Result 556 | where 557 | D: Deserializer<'de>, 558 | { 559 | struct EcoStringVisitor; 560 | 561 | impl Visitor<'_> for EcoStringVisitor { 562 | type Value = EcoString; 563 | 564 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 565 | formatter.write_str("a string") 566 | } 567 | 568 | fn visit_str(self, v: &str) -> Result 569 | where 570 | E: Error, 571 | { 572 | Ok(EcoString::from(v)) 573 | } 574 | 575 | fn visit_bytes(self, v: &[u8]) -> Result 576 | where 577 | E: Error, 578 | { 579 | if let Ok(utf8) = core::str::from_utf8(v) { 580 | return Ok(EcoString::from(utf8)); 581 | } 582 | Err(Error::invalid_value(Unexpected::Bytes(v), &self)) 583 | } 584 | } 585 | 586 | deserializer.deserialize_str(EcoStringVisitor) 587 | } 588 | } 589 | } 590 | -------------------------------------------------------------------------------- /src/vec.rs: -------------------------------------------------------------------------------- 1 | //! A clone-on-write alternative to [`Vec`]. 2 | 3 | use core::alloc::Layout; 4 | use core::array; 5 | use core::borrow::Borrow; 6 | use core::cmp::Ordering; 7 | use core::fmt::{self, Debug, Formatter}; 8 | use core::hash::{Hash, Hasher}; 9 | use core::marker::PhantomData; 10 | use core::mem; 11 | use core::ops::Deref; 12 | use core::ptr::{self, NonNull}; 13 | 14 | #[cfg(not(feature = "std"))] 15 | use alloc::vec::Vec; 16 | 17 | use crate::sync::atomic::{self, AtomicUsize, Ordering::*}; 18 | 19 | /// Create a new [`EcoVec`] with the given elements. 20 | /// ``` 21 | /// # use ecow::eco_vec; 22 | /// assert_eq!(eco_vec![1; 4], [1; 4]); 23 | /// assert_eq!(eco_vec![1, 2, 3], [1, 2, 3]); 24 | /// ``` 25 | #[macro_export] 26 | macro_rules! eco_vec { 27 | () => { $crate::EcoVec::new() }; 28 | ($elem:expr; $n:expr) => { $crate::EcoVec::from_elem($elem, $n) }; 29 | ($($value:expr),+ $(,)?) => { $crate::EcoVec::from([$($value),+]) }; 30 | } 31 | 32 | /// An economical vector with clone-on-write semantics. 33 | /// 34 | /// This type has the same layout as a slice `&[T]`: It consists of a pointer 35 | /// and a length. The pointer is null-pointer optimized (meaning that 36 | /// [`Option>`] has the same size as `EcoVec`). Dereferencing an 37 | /// `EcoVec` to a slice is a no-op. 38 | /// 39 | /// Within its allocation, an `EcoVec` stores a reference count and its 40 | /// capacity. In contrast to an [`Arc>`](alloc::sync::Arc), it only 41 | /// requires a single allocation for both the reference count and the elements. 42 | /// The internal reference counter is atomic, making this type [`Sync`] and 43 | /// [`Send`]. 44 | /// 45 | /// Note that most mutating methods require [`T: Clone`](Clone) due to 46 | /// clone-on-write semantics. 47 | /// 48 | /// # Example 49 | /// ``` 50 | /// use ecow::EcoVec; 51 | /// 52 | /// // Empty vector does not allocate, but first push does. 53 | /// let mut first = EcoVec::new(); 54 | /// first.push(1); 55 | /// first.push(2); 56 | /// assert_eq!(first, [1, 2]); 57 | /// 58 | /// // This clone is cheap, it references the same allocation. 59 | /// let mut second = first.clone(); 60 | /// 61 | /// // This makes a real copy (clone-on-write). 62 | /// second.push(3); 63 | /// assert_eq!(second, [1, 2, 3]); 64 | /// 65 | /// // As `second` was cloned upon mutation, this iterator can 66 | /// // move the elements. If the allocation was still shared with 67 | /// // `first`, this would clone lazily. 68 | /// assert_eq!(second.into_iter().collect::>(), vec![1, 2, 3]); 69 | /// ``` 70 | #[repr(C)] 71 | pub struct EcoVec { 72 | /// Is `Self::dangling()` when the vector is unallocated. 73 | /// 74 | /// Otherwise, points `Self::offset()` bytes after a valid allocation and 75 | /// header, to the start of the vector's elements. It is then aligned to the 76 | /// maximum of the header's alignment and T's alignment. The pointer is 77 | /// valid for `len` reads and `capacity` writes of T. The elements may only 78 | /// be accessed mutably if the reference-count is `1`. 79 | ptr: NonNull, 80 | /// The number of elements in the vector. 81 | /// 82 | /// Invariant: `len <= capacity`. 83 | len: usize, 84 | /// See Vec's impl for more details. 85 | phantom: PhantomData, 86 | } 87 | 88 | /// The start of the backing allocation. 89 | /// 90 | /// This is followed by padding, if necessary, and then the actual data. 91 | #[derive(Debug)] 92 | struct Header { 93 | /// The vector's reference count. Starts at 1 and only drops to zero 94 | /// when the last vector is dropped. 95 | /// 96 | /// Invariant: `refs <= isize::MAX`. 97 | refs: AtomicUsize, 98 | /// The number of elements the backing allocation can hold. Zero if there 99 | /// is no backing allocation. 100 | /// 101 | /// May only be mutated if `refs == 1`. 102 | /// 103 | /// Invariant: `capacity <= isize::MAX`. 104 | capacity: usize, 105 | } 106 | 107 | impl EcoVec { 108 | /// Create a new, empty vector. 109 | /// 110 | /// This does not allocate. 111 | #[inline] 112 | pub const fn new() -> Self { 113 | Self { 114 | ptr: Self::dangling(), 115 | len: 0, 116 | phantom: PhantomData, 117 | } 118 | } 119 | 120 | /// Create a new, empty vector with at least the specified capacity. 121 | #[inline] 122 | pub fn with_capacity(capacity: usize) -> Self { 123 | let mut vec = Self::new(); 124 | if capacity > 0 { 125 | unsafe { 126 | // Safety: 127 | // - The reference count starts at 1. 128 | // - The capacity starts at 0 and the target capacity is checked 129 | // to be `> 0`. 130 | vec.grow(capacity); 131 | } 132 | } 133 | vec 134 | } 135 | 136 | /// Returns `true` if the vector contains no elements. 137 | #[inline] 138 | pub const fn is_empty(&self) -> bool { 139 | self.len == 0 140 | } 141 | 142 | /// The number of elements in the vector. 143 | #[inline] 144 | pub const fn len(&self) -> usize { 145 | self.len 146 | } 147 | 148 | /// How many elements the vector's backing allocation can hold. 149 | /// 150 | /// Even if `len < capacity`, pushing into the vector may still 151 | /// allocate if the reference count is larger than one. 152 | #[inline] 153 | pub fn capacity(&self) -> usize { 154 | self.header().map_or(0, |header| header.capacity) 155 | } 156 | 157 | /// Extracts a slice containing the entire vector. 158 | #[inline] 159 | pub fn as_slice(&self) -> &[T] { 160 | // Safety: 161 | // - The pointer returned by `data()` is non-null, well-aligned, and 162 | // valid for `len` reads of `T`. 163 | // - We have the invariant `len <= capacity <= isize::MAX`. 164 | // - The memory referenced by the slice isn't mutated for the returned 165 | // slice's lifetime, because `self` becomes borrowed and even if there 166 | // are other vectors referencing the same backing allocation, they are 167 | // now allowed to mutate the slice since then the ref-count is larger 168 | // than one. 169 | unsafe { core::slice::from_raw_parts(self.data(), self.len) } 170 | } 171 | 172 | /// Removes all values from the vector. 173 | pub fn clear(&mut self) { 174 | // Nothing to do if it's empty. 175 | if self.is_empty() { 176 | return; 177 | } 178 | 179 | // If there are other vectors that reference the same backing 180 | // allocation, we just create a new, empty vector. 181 | if !self.is_unique() { 182 | // If another vector was dropped in the meantime, this vector could 183 | // have become unique, but we don't care, creating a new one 184 | // is safe nonetheless. Note that this runs the vector's drop 185 | // impl and reduces the ref-count. 186 | *self = Self::new(); 187 | return; 188 | } 189 | 190 | unsafe { 191 | let prev = self.len; 192 | self.len = 0; 193 | 194 | // Safety: 195 | // - We set the length to zero first in case a drop panics, so we 196 | // leak rather than double dropping. 197 | // - We have unique ownership of the backing allocation, so we can 198 | // keep it and clear it. In particular, no other vector can have 199 | // gained shared ownership in the meantime since `is_unique()`, 200 | // as this is the only live vector available for cloning and we 201 | // hold a mutable reference to it. 202 | // - The pointer returned by `data_mut()` is valid for `capacity` 203 | // writes, we have the invariant `prev <= capacity` and thus, 204 | // `data_mut()` is valid for `prev` writes. 205 | ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.data_mut(), prev)); 206 | } 207 | } 208 | } 209 | 210 | impl EcoVec { 211 | /// Create a new vector with `n` copies of `value`. 212 | pub fn from_elem(value: T, n: usize) -> Self { 213 | let mut vec = Self::with_capacity(n); 214 | for _ in 0..n { 215 | // Safety: we just called `EcoVec::with_capacity()` 216 | unsafe { vec.push_unchecked(value.clone()) } 217 | } 218 | vec 219 | } 220 | 221 | /// Produce a mutable slice containing the entire vector. 222 | /// 223 | /// Clones the vector if its reference count is larger than 1. 224 | pub fn make_mut(&mut self) -> &mut [T] { 225 | // To provide mutable access, we must have unique ownership over the 226 | // backing allocation. 227 | self.make_unique(); 228 | 229 | // Safety: 230 | // The reference count is `1` because of `make_unique`. 231 | // For more details, see `Self::as_slice()`. 232 | unsafe { core::slice::from_raw_parts_mut(self.data_mut(), self.len) } 233 | } 234 | 235 | /// Add a value at the end of the vector. 236 | /// 237 | /// Clones the vector if its reference count is larger than 1. 238 | #[inline] 239 | pub fn push(&mut self, value: T) { 240 | // Ensure unique ownership and grow the vector if necessary. 241 | self.reserve((self.len == self.capacity()) as usize); 242 | 243 | // Safety: we just called `EcoVec::reserve()` 244 | unsafe { 245 | self.push_unchecked(value); 246 | } 247 | } 248 | 249 | /// Add a value at the end of the vector, without reallocating. 250 | /// 251 | /// You must ensure that `self.is_unique()` and `self.len < self.capacity()` 252 | /// hold, by calling `EcoVec::with_capacity()` or `EcoVec::reserve()`. 253 | #[inline] 254 | unsafe fn push_unchecked(&mut self, value: T) { 255 | debug_assert!(self.is_unique()); 256 | debug_assert!(self.len < self.capacity()); 257 | 258 | unsafe { 259 | // Safety: 260 | // - The caller must ensure that the reference count is `1`. 261 | // - The pointer returned by `data_mut()` is valid for `capacity` 262 | // writes. 263 | // - The caller must ensure that `len < capacity`. 264 | // - Thus, `data_mut() + len` is valid for one write. 265 | ptr::write(self.data_mut().add(self.len), value); 266 | 267 | // Safety: 268 | // Since we reserved space, we maintain `len <= capacity`. 269 | self.len += 1; 270 | } 271 | } 272 | 273 | /// Removes the last element from a vector and returns it, or `None` if the 274 | /// vector is empty. 275 | /// 276 | /// Clones the vector if its reference count is larger than 1. 277 | #[inline] 278 | pub fn pop(&mut self) -> Option { 279 | if self.is_empty() { 280 | return None; 281 | } 282 | 283 | self.make_unique(); 284 | 285 | unsafe { 286 | // Safety: 287 | // Cannot underflow because `is_empty` returned `false`. 288 | self.len -= 1; 289 | 290 | // Safety: 291 | // - The reference count is `1` because of `make_unique`. 292 | // - The pointer returned by `data()` is valid for `len` reads and 293 | // thus `data() + new_len` is valid for one read. 294 | Some(ptr::read(self.data().add(self.len))) 295 | } 296 | } 297 | 298 | /// Inserts an element at an index within the vector, shifting all elements 299 | /// after it to the right. 300 | /// 301 | /// Clones the vector if its reference count is larger than 1. 302 | /// 303 | /// Panics if `index > len`. 304 | pub fn insert(&mut self, index: usize, value: T) { 305 | if index > self.len { 306 | out_of_bounds(index, self.len); 307 | } 308 | 309 | // Ensure unique ownership and grow the vector if necessary. 310 | self.reserve((self.len == self.capacity()) as usize); 311 | 312 | unsafe { 313 | // Safety: 314 | // - The reference count is `1` because of `reserve`. 315 | // - The pointer returned by `data_mut()` is valid for `len` 316 | // reads and `capacity` writes of `T`. 317 | // - Thus, `at` is valid for `len - index` reads of `T` 318 | // - And `at` is valid for `capacity - index` writes of `T`. 319 | // Because of the `reserve` call, we have `len < capacity` and 320 | // thus `at + 1` is valid for `len - index` writes of `T`. 321 | let at = self.data_mut().add(index); 322 | ptr::copy(at, at.add(1), self.len - index); 323 | 324 | // Safety: 325 | // - The pointer returned by `data_mut()` is valid for `capacity` 326 | // writes. 327 | // - Due to the bounds check above, `index <= len` 328 | // - Due to the reserve check, `len < capacity`. 329 | // - Thus, `data() + index` is valid for one write. 330 | ptr::write(at, value); 331 | 332 | // Safety: 333 | // Since we reserved space, we maintain `len <= capacity`. 334 | self.len += 1; 335 | } 336 | } 337 | 338 | /// Removes and returns the element at position index within the vector, 339 | /// shifting all elements after it to the left. 340 | /// 341 | /// Clones the vector if its reference count is larger than 1. 342 | /// 343 | /// Panics if `index >= len`. 344 | pub fn remove(&mut self, index: usize) -> T { 345 | if index >= self.len { 346 | out_of_bounds(index, self.len); 347 | } 348 | 349 | self.make_unique(); 350 | 351 | unsafe { 352 | // Safety: 353 | // - The reference count is `1` because of `make_unique`. 354 | // - The pointer returned by `data()` is valid for `len` reads. 355 | // - Due to the check above, `index < len`. 356 | // - Thus, `at` is valid for one read. 357 | let at = self.data_mut().add(index); 358 | let value = ptr::read(at); 359 | 360 | // Safety: 361 | // - The pointer returned by `data()` is valid for `len` reads and 362 | // `capacity` writes. 363 | // - Thus, `at + 1` is valid for `len - index - 1` reads. 364 | // - Thus, `at` is valid for `capacity - index` writes. 365 | // - Due to the invariant `len <= capacity`, `at` is also valid 366 | // for `len - index - 1` writes. 367 | ptr::copy(at.add(1), at, self.len - index - 1); 368 | 369 | // Safety: 370 | // Cannot underflow because `index < len` and thus `len > 0`. 371 | self.len -= 1; 372 | 373 | value 374 | } 375 | } 376 | 377 | /// Retains only the elements specified by the predicate. 378 | /// 379 | /// Clones the vector if its reference count is larger than 1. 380 | /// 381 | /// Note that this clones the vector even if `f` always returns `false`. To 382 | /// prevent that, you can first iterate over the vector yourself and then 383 | /// only call `retain` if your condition is `false` for some element. 384 | pub fn retain(&mut self, mut f: F) 385 | where 386 | F: FnMut(&mut T) -> bool, 387 | { 388 | // Modified from: https://github.com/servo/rust-smallvec 389 | // Copyright (c) 2018 The Servo Project Developers 390 | let len = self.len; 391 | let values = self.make_mut(); 392 | 393 | let mut del = 0; 394 | for i in 0..len { 395 | if !f(&mut values[i]) { 396 | del += 1; 397 | } else if del > 0 { 398 | values.swap(i - del, i); 399 | } 400 | } 401 | 402 | if del > 0 { 403 | self.truncate(len - del); 404 | } 405 | } 406 | 407 | /// Shortens the vector, keeping the first `target` elements and dropping 408 | /// the rest. 409 | /// 410 | /// Clones the vector if its reference count is larger than 1 and 411 | /// `target < len`. 412 | pub fn truncate(&mut self, target: usize) { 413 | if target >= self.len { 414 | return; 415 | } 416 | 417 | if !self.is_unique() { 418 | // Safety: Just checked bounds. 419 | *self = Self::from(unsafe { self.get_unchecked(..target) }); 420 | return; 421 | } 422 | 423 | let rest = self.len - target; 424 | unsafe { 425 | // Safety: 426 | // - Since `target < len`, we maintain `len <= capacity`. 427 | self.len = target; 428 | 429 | // Safety: 430 | // The reference count is `1` because of `make_unique`. 431 | // - The pointer returned by `data_mut()` is valid for `capacity` 432 | // writes. 433 | // - We have the invariant `len <= capacity`. 434 | // - Thus, `data_mut() + target` is valid for `len - target` writes. 435 | ptr::drop_in_place(ptr::slice_from_raw_parts_mut( 436 | self.data_mut().add(target), 437 | rest, 438 | )); 439 | } 440 | } 441 | 442 | /// Reserve space for at least `additional` more elements. 443 | /// 444 | /// Guarantees that the resulting vector has reference count `1` and space 445 | /// for `additional` more elements. 446 | pub fn reserve(&mut self, additional: usize) { 447 | let capacity = self.capacity(); 448 | let mut target = capacity; 449 | 450 | if additional > capacity - self.len { 451 | // Reserve at least the `additional` capacity, but also at least 452 | // double the capacity to ensure exponential growth and finally 453 | // jump directly to a minimum capacity to prevent frequent 454 | // reallocation for small vectors. 455 | target = self 456 | .len 457 | .checked_add(additional) 458 | .unwrap_or_else(|| capacity_overflow()) 459 | .max(2 * capacity) 460 | .max(Self::min_cap()); 461 | } 462 | 463 | if !self.is_unique() { 464 | let mut vec = Self::with_capacity(target); 465 | vec.extend(self.iter().cloned()); 466 | *self = vec; 467 | } else if target > capacity { 468 | unsafe { 469 | // Safety: 470 | // - The reference count is `1` because of `make_unique`. 471 | // - The `target` capacity is greater than the current capacity 472 | // because `additional > 0`. 473 | self.grow(target); 474 | } 475 | } 476 | } 477 | 478 | /// Clones and pushes all elements in a slice to the vector. 479 | pub fn extend_from_slice(&mut self, slice: &[T]) { 480 | if slice.is_empty() { 481 | return; 482 | } 483 | 484 | self.reserve(slice.len()); 485 | 486 | for value in slice { 487 | // Safety: 488 | // - The reference count is `1` because of `reserve`. 489 | // - `self.len < self.capacity()` because we reserved space for 490 | // `slice.len()` more elements. 491 | unsafe { 492 | self.push_unchecked(value.clone()); 493 | } 494 | } 495 | } 496 | 497 | /// Pushes all elements in a trusted-len iterator to the vector. 498 | /// 499 | /// # Safety 500 | /// We can't use `TrustedLen` because it is unstable. Still, the 501 | /// `ExactSizeIterator::len` must return the exact length of the iterator 502 | /// for this to be safe. 503 | pub unsafe fn extend_from_trusted(&mut self, iter: I) 504 | where 505 | I: IntoIterator, 506 | I::IntoIter: ExactSizeIterator, 507 | { 508 | let iter = iter.into_iter(); 509 | let count = iter.len(); 510 | 511 | if count == 0 { 512 | return; 513 | } 514 | 515 | self.reserve(count); 516 | 517 | for value in iter { 518 | // Safety: 519 | // - The reference count is `1` because of `reserve`. 520 | // - `self.len < self.capacity()` because we reserved space for 521 | // `iter.len()` more elements. 522 | unsafe { 523 | self.push_unchecked(value); 524 | } 525 | } 526 | } 527 | } 528 | 529 | impl EcoVec { 530 | /// Grow the capacity to at least the `target` size. 531 | /// 532 | /// May only be called if: 533 | /// - the reference count is `1`, and 534 | /// - `target > capacity` (i.e., this methods grows, it doesn't shrink). 535 | unsafe fn grow(&mut self, mut target: usize) { 536 | debug_assert!(self.is_unique()); 537 | debug_assert!(target > self.capacity()); 538 | 539 | // Maintain the `capacity <= isize::MAX` invariant. 540 | if target > isize::MAX as usize { 541 | capacity_overflow(); 542 | } 543 | 544 | // Directly go to maximum capacity for ZSTs. 545 | if mem::size_of::() == 0 { 546 | target = isize::MAX as usize; 547 | } 548 | 549 | let layout = Self::layout(target); 550 | let allocation = if !self.is_allocated() { 551 | // Safety: 552 | // The layout has non-zero size because `target > 0`. 553 | alloc::alloc::alloc(layout) 554 | } else { 555 | // Safety: 556 | // - `self.ptr` was allocated before (just checked) 557 | // - the old block was allocated with the current capacity 558 | // - `Self::size()` guarantees to return a value that is `> 0` 559 | // and rounded up to the nearest multiple of `Self::align()` 560 | // does not overflow `isize::MAX`. 561 | alloc::alloc::realloc( 562 | self.allocation_mut(), 563 | Self::layout(self.capacity()), 564 | Self::size(target), 565 | ) 566 | }; 567 | 568 | if allocation.is_null() { 569 | alloc::alloc::handle_alloc_error(layout); 570 | } 571 | 572 | // Construct data pointer by offsetting. 573 | // 574 | // Safety: 575 | // Just checked for null and adding only increases the size. Can't 576 | // overflow because the `allocation` is a valid pointer to 577 | // `Self::size(target)` bytes and `Self::offset() < Self::size(target)`. 578 | self.ptr = NonNull::new_unchecked(allocation.add(Self::offset()).cast()); 579 | debug_assert_ne!(self.ptr, Self::dangling()); 580 | 581 | // Safety: 582 | // The freshly allocated pointer is valid for a write of the header. 583 | ptr::write( 584 | allocation.cast::
(), 585 | Header { refs: AtomicUsize::new(1), capacity: target }, 586 | ); 587 | } 588 | 589 | /// Whether this vector has a backing allocation. 590 | #[inline] 591 | fn is_allocated(&self) -> bool { 592 | !ptr::eq(self.ptr.as_ptr(), Self::dangling().as_ptr()) 593 | } 594 | 595 | /// An immutable pointer to the backing allocation. 596 | /// 597 | /// May only be called if `is_allocated` returns `true`. 598 | #[inline] 599 | unsafe fn allocation(&self) -> *const u8 { 600 | debug_assert!(self.is_allocated()); 601 | self.ptr.as_ptr().cast::().sub(Self::offset()) 602 | } 603 | 604 | /// A mutable pointer to the backing allocation. 605 | /// 606 | /// May only be called if `is_allocated` returns `true`. 607 | #[inline] 608 | unsafe fn allocation_mut(&mut self) -> *mut u8 { 609 | debug_assert!(self.is_allocated()); 610 | self.ptr.as_ptr().cast::().sub(Self::offset()) 611 | } 612 | 613 | /// A reference to the header. 614 | #[inline] 615 | fn header(&self) -> Option<&Header> { 616 | // Safety: 617 | // If the vector is allocated, there is always a valid header. 618 | self.is_allocated() 619 | .then(|| unsafe { &*self.allocation().cast::
() }) 620 | } 621 | 622 | /// The data pointer. 623 | /// 624 | /// Returns a pointer that is non-null, well-aligned, and valid for `len` 625 | /// reads of `T`. 626 | #[inline] 627 | fn data(&self) -> *const T { 628 | self.ptr.as_ptr() 629 | } 630 | 631 | /// The data pointer, mutably. 632 | /// 633 | /// Returns a pointer that is non-null, well-aligned, and valid for 634 | /// `capacity` writes of `T`. 635 | /// 636 | /// May only be called if the reference count is 1. 637 | #[inline] 638 | unsafe fn data_mut(&mut self) -> *mut T { 639 | self.ptr.as_ptr() 640 | } 641 | 642 | /// The layout of a backing allocation for the given capacity. 643 | #[inline] 644 | fn layout(capacity: usize) -> Layout { 645 | // Safety: 646 | // - `Self::size(capacity)` guarantees that it rounded up the alignment 647 | // does not overflow `isize::MAX`. 648 | // - Since `Self::align()` is the header's alignment or T's alignment, 649 | // it fulfills the requirements of a valid alignment. 650 | unsafe { Layout::from_size_align_unchecked(Self::size(capacity), Self::align()) } 651 | } 652 | 653 | /// The size of a backing allocation for the given capacity. 654 | /// 655 | /// Always `> 0`. When rounded up to the next multiple of `Self::align()` is 656 | /// guaranteed to be `<= isize::MAX`. 657 | #[inline] 658 | fn size(capacity: usize) -> usize { 659 | mem::size_of::() 660 | .checked_mul(capacity) 661 | .and_then(|size| Self::offset().checked_add(size)) 662 | .filter(|&size| { 663 | // See `Layout::max_size_for_align` for details. 664 | size < isize::MAX as usize - Self::align() 665 | }) 666 | .unwrap_or_else(|| capacity_overflow()) 667 | } 668 | 669 | /// The alignment of the backing allocation. 670 | #[inline] 671 | const fn align() -> usize { 672 | max(mem::align_of::
(), mem::align_of::()) 673 | } 674 | 675 | /// The offset of the data in the backing allocation. 676 | /// 677 | /// Always `> 0`. `self.ptr` points to the data and `self.ptr - offset` to 678 | /// the header. 679 | #[inline] 680 | const fn offset() -> usize { 681 | max(mem::size_of::
(), Self::align()) 682 | } 683 | 684 | /// The sentinel value of `self.ptr`, used to indicate an uninitialized, 685 | /// unallocated vector. It is dangling (does not point to valid memory) and 686 | /// has no provenance. As such, it must not be used to read/write/offset. 687 | /// However, it is well-aligned, so it can be used to create 0-length 688 | /// slices. 689 | /// 690 | /// All pointers to allocated vector elements will be distinct from this 691 | /// value, because allocated vector elements start `Self::offset()` bytes 692 | /// into a heap allocation and heap allocations cannot start at 0 (null). 693 | #[inline] 694 | const fn dangling() -> NonNull { 695 | unsafe { 696 | // Safety: This is the stable equivalent of `core::ptr::invalid_mut`. 697 | // The pointer we create has no provenance and may not be 698 | // read/write/offset. 699 | #[allow(clippy::useless_transmute)] 700 | let ptr = mem::transmute::(Self::offset()); 701 | 702 | // Safety: `Self::offset()` is never 0. 703 | NonNull::new_unchecked(ptr) 704 | } 705 | } 706 | 707 | /// The minimum non-zero capacity. 708 | #[inline] 709 | const fn min_cap() -> usize { 710 | // In the spirit of the `EcoVec`, we choose the cutoff size of T from 711 | // which 1 is the minimum capacity a bit lower than a standard `Vec`. 712 | if mem::size_of::() == 1 { 713 | 8 714 | } else if mem::size_of::() <= 32 { 715 | 4 716 | } else { 717 | 1 718 | } 719 | } 720 | } 721 | 722 | impl EcoVec { 723 | /// Ensure that this vector has a unique backing allocation. 724 | /// 725 | /// May change the capacity. 726 | fn make_unique(&mut self) { 727 | if !self.is_unique() { 728 | *self = Self::from(self.as_slice()); 729 | } 730 | } 731 | } 732 | 733 | impl EcoVec { 734 | /// Copies from a byte slice. 735 | #[inline] 736 | pub(crate) fn extend_from_byte_slice(&mut self, bytes: &[u8]) { 737 | if bytes.is_empty() { 738 | return; 739 | } 740 | 741 | self.reserve(bytes.len()); 742 | 743 | unsafe { 744 | // Safety: 745 | // - The source slice is valid for `bytes.len()` reads. 746 | // - The destination is valid for `bytes.len()` more writes due to 747 | // the `reserve` call. 748 | // - The two ranges are non-overlapping because we hold a mutable 749 | // reference to `self` and an immutable one to `bytes`. 750 | ptr::copy_nonoverlapping( 751 | bytes.as_ptr(), 752 | self.data_mut().add(self.len), 753 | bytes.len(), 754 | ); 755 | } 756 | 757 | self.len += bytes.len(); 758 | } 759 | } 760 | 761 | // Safety: Works like `Arc`. 762 | unsafe impl Sync for EcoVec {} 763 | unsafe impl Send for EcoVec {} 764 | 765 | impl EcoVec { 766 | /// Whether no other vector is pointing to the same backing allocation. 767 | /// 768 | /// This takes a mutable reference because only callers with ownership or a 769 | /// mutable reference can ensure that the result stays relevant. Potential 770 | /// callers with a shared reference could read `true` while another shared 771 | /// reference is cloned on a different thread, bumping the ref-count. By 772 | /// restricting this callers with mutable access, we ensure that no 773 | /// uncontrolled cloning is happening in the time between the `is_unique` 774 | /// call and any subsequent mutation. 775 | #[inline] 776 | pub fn is_unique(&mut self) -> bool { 777 | // See Arc's is_unique() method. 778 | self.header().map_or(true, |header| header.refs.load(Acquire) == 1) 779 | } 780 | } 781 | 782 | impl Clone for EcoVec { 783 | #[inline] 784 | fn clone(&self) -> Self { 785 | // If the vector has a backing allocation, bump the ref-count. 786 | if let Some(header) = self.header() { 787 | // See Arc's clone impl for details about memory ordering. 788 | let prev = header.refs.fetch_add(1, Relaxed); 789 | 790 | // See Arc's clone impl details about guarding against incredibly degenerate programs 791 | if prev > isize::MAX as usize { 792 | ref_count_overflow(self.ptr, self.len); 793 | } 794 | } 795 | 796 | Self { ptr: self.ptr, len: self.len, phantom: PhantomData } 797 | } 798 | } 799 | 800 | impl Drop for EcoVec { 801 | fn drop(&mut self) { 802 | // Drop our ref-count. If there was more than one vector before 803 | // (including this one), we shouldn't deallocate. Nothing to do if there 804 | // is no header and thus no backing allocation. See Arc's drop impl for 805 | // details about memory ordering. 806 | if self 807 | .header() 808 | .map_or(true, |header| header.refs.fetch_sub(1, Release) != 1) 809 | { 810 | return; 811 | } 812 | 813 | // See Arc's drop impl for details. 814 | atomic::fence(Acquire); 815 | 816 | // Ensures that the backing storage is deallocated even if one of the 817 | // element drops panics. 818 | struct Dealloc(*mut u8, Layout); 819 | 820 | impl Drop for Dealloc { 821 | fn drop(&mut self) { 822 | // Safety: See below. 823 | unsafe { 824 | alloc::alloc::dealloc(self.0, self.1); 825 | } 826 | } 827 | } 828 | 829 | // Safety: 830 | // The vector has a header, so `self.allocation()` points to an 831 | // allocation with the layout of current capacity. 832 | let _dealloc = 833 | unsafe { Dealloc(self.allocation_mut(), Self::layout(self.capacity())) }; 834 | 835 | unsafe { 836 | // Safety: 837 | // No other vector references the backing allocation (just checked). 838 | // For more details, see `Self::as_slice()`. 839 | ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.data_mut(), self.len)); 840 | } 841 | } 842 | } 843 | 844 | impl Deref for EcoVec { 845 | type Target = [T]; 846 | 847 | #[inline] 848 | fn deref(&self) -> &Self::Target { 849 | self.as_slice() 850 | } 851 | } 852 | 853 | impl Borrow<[T]> for EcoVec { 854 | #[inline] 855 | fn borrow(&self) -> &[T] { 856 | self.as_slice() 857 | } 858 | } 859 | 860 | impl AsRef<[T]> for EcoVec { 861 | #[inline] 862 | fn as_ref(&self) -> &[T] { 863 | self.as_slice() 864 | } 865 | } 866 | 867 | impl Default for EcoVec { 868 | #[inline] 869 | fn default() -> Self { 870 | Self::new() 871 | } 872 | } 873 | 874 | impl Debug for EcoVec { 875 | #[inline] 876 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { 877 | self.as_slice().fmt(f) 878 | } 879 | } 880 | 881 | impl Hash for EcoVec { 882 | #[inline] 883 | fn hash(&self, state: &mut H) { 884 | self.as_slice().hash(state); 885 | } 886 | } 887 | 888 | impl Eq for EcoVec {} 889 | 890 | impl PartialEq for EcoVec { 891 | #[inline] 892 | fn eq(&self, other: &Self) -> bool { 893 | self.as_slice() == other.as_slice() 894 | } 895 | } 896 | 897 | impl PartialEq<[T]> for EcoVec { 898 | #[inline] 899 | fn eq(&self, other: &[T]) -> bool { 900 | self.as_slice() == other 901 | } 902 | } 903 | 904 | impl PartialEq<&[T]> for EcoVec { 905 | #[inline] 906 | fn eq(&self, other: &&[T]) -> bool { 907 | self.as_slice() == *other 908 | } 909 | } 910 | 911 | impl PartialEq<[T; N]> for EcoVec { 912 | #[inline] 913 | fn eq(&self, other: &[T; N]) -> bool { 914 | self.as_slice() == other 915 | } 916 | } 917 | 918 | impl PartialEq<&[T; N]> for EcoVec { 919 | #[inline] 920 | fn eq(&self, other: &&[T; N]) -> bool { 921 | self.as_slice() == *other 922 | } 923 | } 924 | 925 | impl PartialEq> for EcoVec { 926 | #[inline] 927 | fn eq(&self, other: &Vec) -> bool { 928 | self.as_slice() == other 929 | } 930 | } 931 | 932 | impl PartialEq> for [T] { 933 | #[inline] 934 | fn eq(&self, other: &EcoVec) -> bool { 935 | self == other.as_slice() 936 | } 937 | } 938 | 939 | impl PartialEq> for [T; N] { 940 | #[inline] 941 | fn eq(&self, other: &EcoVec) -> bool { 942 | self == other.as_slice() 943 | } 944 | } 945 | 946 | impl PartialEq> for Vec { 947 | #[inline] 948 | fn eq(&self, other: &EcoVec) -> bool { 949 | self == other.as_slice() 950 | } 951 | } 952 | 953 | impl Ord for EcoVec { 954 | #[inline] 955 | fn cmp(&self, other: &Self) -> Ordering { 956 | self.as_slice().cmp(other.as_slice()) 957 | } 958 | } 959 | 960 | impl PartialOrd for EcoVec { 961 | #[inline] 962 | fn partial_cmp(&self, other: &Self) -> Option { 963 | self.as_slice().partial_cmp(other.as_slice()) 964 | } 965 | } 966 | 967 | impl From<&[T]> for EcoVec { 968 | fn from(slice: &[T]) -> Self { 969 | let mut vec = Self::new(); 970 | vec.extend_from_slice(slice); 971 | vec 972 | } 973 | } 974 | 975 | impl From<[T; N]> for EcoVec { 976 | fn from(array: [T; N]) -> Self { 977 | let mut vec = Self::new(); 978 | unsafe { 979 | // Safety: Array's IntoIter implements `TrustedLen`. 980 | vec.extend_from_trusted(array); 981 | } 982 | vec 983 | } 984 | } 985 | 986 | impl From> for EcoVec { 987 | /// This needs to allocate to change the layout. 988 | fn from(other: Vec) -> Self { 989 | let mut vec = Self::new(); 990 | unsafe { 991 | // Safety: Vec's IntoIter implements `TrustedLen`. 992 | vec.extend_from_trusted(other); 993 | } 994 | vec 995 | } 996 | } 997 | 998 | impl TryFrom> for [T; N] { 999 | type Error = EcoVec; 1000 | 1001 | fn try_from(mut vec: EcoVec) -> Result { 1002 | if vec.len() != N { 1003 | return Err(vec); 1004 | } 1005 | 1006 | Ok(if vec.is_unique() { 1007 | // Set the length to zero to prevent double drop. 1008 | vec.len = 0; 1009 | 1010 | // Safety: 1011 | // - We have unique ownership over the underlying allocation. 1012 | // - The pointer returned by `data()` is valid for `N` reads. 1013 | // - We take ownership of the value and don't drop it again 1014 | // in our drop impl. 1015 | unsafe { ptr::read(vec.data() as *const [T; N]) } 1016 | } else { 1017 | // Safety: We know that the length is correct. 1018 | unsafe { array::from_fn(|i| vec.get_unchecked(i).clone()) } 1019 | }) 1020 | } 1021 | } 1022 | 1023 | impl FromIterator for EcoVec { 1024 | fn from_iter>(iter: I) -> Self { 1025 | let iter = iter.into_iter(); 1026 | let hint = iter.size_hint().0; 1027 | let mut vec = Self::with_capacity(hint); 1028 | vec.extend(iter); 1029 | vec 1030 | } 1031 | } 1032 | 1033 | impl Extend for EcoVec { 1034 | fn extend(&mut self, iter: I) 1035 | where 1036 | I: IntoIterator, 1037 | { 1038 | let iter = iter.into_iter(); 1039 | let hint = iter.size_hint().0; 1040 | if hint > 0 { 1041 | self.reserve(hint); 1042 | } 1043 | for value in iter { 1044 | self.push(value); 1045 | } 1046 | } 1047 | } 1048 | 1049 | impl<'a, T> IntoIterator for &'a EcoVec { 1050 | type IntoIter = core::slice::Iter<'a, T>; 1051 | type Item = &'a T; 1052 | 1053 | #[inline] 1054 | fn into_iter(self) -> Self::IntoIter { 1055 | self.as_slice().iter() 1056 | } 1057 | } 1058 | 1059 | impl IntoIterator for EcoVec { 1060 | type IntoIter = IntoIter; 1061 | type Item = T; 1062 | 1063 | #[inline] 1064 | fn into_iter(mut self) -> Self::IntoIter { 1065 | IntoIter { 1066 | unique: self.is_unique(), 1067 | front: 0, 1068 | back: self.len, 1069 | vec: self, 1070 | } 1071 | } 1072 | } 1073 | 1074 | /// An owned iterator over an [`EcoVec`]. 1075 | /// 1076 | /// If the vector had a reference count of 1, this moves out of the vector, 1077 | /// otherwise it lazily clones. 1078 | pub struct IntoIter { 1079 | /// The underlying vector. 1080 | vec: EcoVec, 1081 | /// Whether we have unique ownership over the underlying allocation. 1082 | unique: bool, 1083 | /// How many elements we have already read from the front. 1084 | /// If `unique` is true, these must not be dropped in our drop impl! 1085 | /// 1086 | /// Invariant: `0 <= front <= back`. 1087 | front: usize, 1088 | /// How many elements we have already read from the back. 1089 | /// If `unique` is true, these must not be dropped in our drop impl! 1090 | /// 1091 | /// Invariant: `0 <= back <= len`. 1092 | back: usize, 1093 | } 1094 | 1095 | impl IntoIter { 1096 | /// Returns the remaining items of this iterator as a slice. 1097 | #[inline] 1098 | pub fn as_slice(&self) -> &[T] { 1099 | unsafe { 1100 | // Safety: 1101 | // - The pointer returned by `data()` is valid for `len` reads. 1102 | // - Since `front <= back <= len`, `data() + front` is valid for 1103 | // `back - front` reads. 1104 | // - For more details, see `EcoVec::as_slice`. 1105 | core::slice::from_raw_parts( 1106 | self.vec.data().add(self.front), 1107 | self.back - self.front, 1108 | ) 1109 | } 1110 | } 1111 | } 1112 | 1113 | impl Iterator for IntoIter { 1114 | type Item = T; 1115 | 1116 | #[inline] 1117 | fn next(&mut self) -> Option { 1118 | (self.front < self.back).then(|| { 1119 | let prev = self.front; 1120 | self.front += 1; 1121 | if self.unique { 1122 | // Safety: 1123 | // - We have unique ownership over the underlying allocation. 1124 | // - The pointer returned by `data()` is valid for `len` reads. 1125 | // - We know that `prev < self.back <= len`. 1126 | // - We take ownership of the value and don't drop it again 1127 | // in our drop impl. 1128 | unsafe { ptr::read(self.vec.data().add(prev)) } 1129 | } else { 1130 | // Safety: 1131 | // - We know that `prev < self.back <= len`. 1132 | unsafe { self.vec.get_unchecked(prev).clone() } 1133 | } 1134 | }) 1135 | } 1136 | 1137 | #[inline] 1138 | fn size_hint(&self) -> (usize, Option) { 1139 | let len = self.back - self.front; 1140 | (len, Some(len)) 1141 | } 1142 | 1143 | #[inline] 1144 | fn count(self) -> usize { 1145 | self.len() 1146 | } 1147 | } 1148 | 1149 | impl DoubleEndedIterator for IntoIter { 1150 | #[inline] 1151 | fn next_back(&mut self) -> Option { 1152 | (self.back > self.front).then(|| { 1153 | self.back -= 1; 1154 | if self.unique { 1155 | // Safety: 1156 | // - We have unique ownership over the underlying allocation. 1157 | // - The pointer returned by `data()` is valid for `len` reads. 1158 | // - We know that `self.back < len` at this point. 1159 | // - We take ownership of the value and don't drop it again 1160 | // in our drop impl. 1161 | unsafe { ptr::read(self.vec.data().add(self.back)) } 1162 | } else { 1163 | // Safety: 1164 | // - Due to the subtraction, `self.back < len` at this point. 1165 | unsafe { self.vec.get_unchecked(self.back).clone() } 1166 | } 1167 | }) 1168 | } 1169 | } 1170 | 1171 | impl ExactSizeIterator for IntoIter {} 1172 | 1173 | impl Drop for IntoIter { 1174 | fn drop(&mut self) { 1175 | if !self.unique || !self.vec.is_allocated() { 1176 | return; 1177 | } 1178 | 1179 | // Safety: 1180 | // We have unique ownership over the underlying allocation. 1181 | unsafe { 1182 | // Safety: 1183 | // Set len to zero before dropping to prevent double dropping in 1184 | // EcoVec's drop impl in case of panic. 1185 | self.vec.len = 0; 1186 | 1187 | // Safety: 1188 | // - The elements in `..self.front` and `self.back..` have 1189 | // already been moved out of the vector. Thus, we only drop 1190 | // the elements that remain in the middle. 1191 | // - For details about the slicing, see `Self::as_slice()`. 1192 | ptr::drop_in_place(ptr::slice_from_raw_parts_mut( 1193 | self.vec.data_mut().add(self.front), 1194 | self.back - self.front, 1195 | )); 1196 | } 1197 | } 1198 | } 1199 | 1200 | impl Debug for IntoIter { 1201 | #[inline] 1202 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { 1203 | f.debug_tuple("IntoIter").field(&self.as_slice()).finish() 1204 | } 1205 | } 1206 | 1207 | #[cold] 1208 | fn capacity_overflow() -> ! { 1209 | panic!("capacity overflow"); 1210 | } 1211 | 1212 | #[cold] 1213 | fn ref_count_overflow(ptr: NonNull, len: usize) -> ! { 1214 | // Drop to decrement the ref count to counter the increment in `clone()` 1215 | drop(EcoVec { ptr, len, phantom: PhantomData }); 1216 | panic!("reference count overflow"); 1217 | } 1218 | 1219 | #[cold] 1220 | fn out_of_bounds(index: usize, len: usize) -> ! { 1221 | panic!("index is out bounds (index: {index}, len: {len})"); 1222 | } 1223 | 1224 | // Copy of `std::cmp::max::()` that is callable in `const` contexts 1225 | #[inline] 1226 | const fn max(x: usize, y: usize) -> usize { 1227 | if x > y { 1228 | x 1229 | } else { 1230 | y 1231 | } 1232 | } 1233 | 1234 | #[cfg(feature = "std")] 1235 | impl std::io::Write for EcoVec { 1236 | #[inline] 1237 | fn write(&mut self, buf: &[u8]) -> std::io::Result { 1238 | self.extend_from_byte_slice(buf); 1239 | Ok(buf.len()) 1240 | } 1241 | 1242 | #[inline] 1243 | fn flush(&mut self) -> std::io::Result<()> { 1244 | Ok(()) 1245 | } 1246 | } 1247 | 1248 | #[cfg(feature = "serde")] 1249 | mod serde { 1250 | use crate::EcoVec; 1251 | use core::{fmt, marker::PhantomData}; 1252 | use serde::de::{Deserializer, Visitor}; 1253 | 1254 | impl serde::Serialize for EcoVec { 1255 | fn serialize(&self, serializer: S) -> Result 1256 | where 1257 | S: serde::Serializer, 1258 | { 1259 | self.as_slice().serialize(serializer) 1260 | } 1261 | } 1262 | 1263 | struct EcoVecVisitor(PhantomData); 1264 | 1265 | impl<'a, T: serde::Deserialize<'a> + Clone> Visitor<'a> for EcoVecVisitor { 1266 | type Value = EcoVec; 1267 | 1268 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 1269 | formatter.write_str("a sequence") 1270 | } 1271 | 1272 | fn visit_seq(self, mut seq: A) -> Result 1273 | where 1274 | A: serde::de::SeqAccess<'a>, 1275 | { 1276 | let len = seq.size_hint().unwrap_or(0); 1277 | let mut values = EcoVec::with_capacity(len); 1278 | while let Some(value) = seq.next_element()? { 1279 | values.push(value) 1280 | } 1281 | Ok(values) 1282 | } 1283 | } 1284 | 1285 | impl<'de, T: serde::Deserialize<'de> + Clone> serde::Deserialize<'de> for EcoVec { 1286 | fn deserialize(deserializer: D) -> Result 1287 | where 1288 | D: Deserializer<'de>, 1289 | { 1290 | deserializer.deserialize_seq(EcoVecVisitor(PhantomData)) 1291 | } 1292 | } 1293 | } 1294 | -------------------------------------------------------------------------------- /tests/loom.rs: -------------------------------------------------------------------------------- 1 | // Run loom tests with something like 2 | // ``` 3 | // RUSTFLAGS="--cfg loom" cargo test --release loom --features loom 4 | // ``` 5 | 6 | // See https://github.com/tokio-rs/loom/issues/352 7 | #![allow(unknown_lints, unexpected_cfgs)] 8 | 9 | #[cfg(loom)] 10 | mod loom { 11 | #[cfg(debug_assertions)] 12 | compile_error!( 13 | "Loom tests are typically slow in debug mode. Run them with `--release`" 14 | ); 15 | 16 | use ecow::eco_vec; 17 | 18 | #[test] 19 | fn smoke() { 20 | loom::model(|| { 21 | let mut one = eco_vec![1, 2, 3]; 22 | let two = one.clone(); 23 | 24 | loom::thread::spawn(move || { 25 | let mut three = two.clone(); 26 | three.push(4); 27 | 28 | assert!(three.len() > two.len()); 29 | }); 30 | 31 | one.push(4); 32 | assert_eq!(one, [1, 2, 3, 4]); 33 | }); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/tests.rs: -------------------------------------------------------------------------------- 1 | // Test with `cargo +nightly miri test` to check sanity! 2 | 3 | #![allow(clippy::redundant_clone)] 4 | #![allow(clippy::disallowed_names)] 5 | 6 | use std::borrow::Cow; 7 | use std::collections::HashMap; 8 | use std::fmt::Write; 9 | use std::mem; 10 | use std::sync::atomic::{AtomicUsize, Ordering::*}; 11 | 12 | use ecow::{eco_vec, EcoString, EcoVec}; 13 | 14 | const ALPH: &str = "abcdefghijklmnopqrstuvwxyz"; 15 | const LIMIT: usize = EcoString::INLINE_LIMIT; 16 | 17 | fn v(value: T) -> Box { 18 | Box::new(value) 19 | } 20 | 21 | #[test] 22 | fn test_mem_size() { 23 | let word = mem::size_of::(); 24 | assert_eq!(mem::size_of::>(), 2 * word); 25 | assert_eq!(mem::size_of::>>(), 2 * word); 26 | 27 | if cfg!(target_endian = "little") { 28 | if cfg!(target_pointer_width = "32") { 29 | // Inline length still should be reasonable on 32-bit systems 30 | assert_eq!(mem::size_of::(), 4 * word); 31 | // No niche :( 32 | assert_eq!(mem::size_of::>(), 5 * word); 33 | } else if cfg!(target_pointer_width = "64") { 34 | assert_eq!(mem::size_of::(), 2 * word); 35 | // No niche :( 36 | assert_eq!(mem::size_of::>(), 3 * word); 37 | } 38 | } 39 | } 40 | 41 | #[test] 42 | fn test_vec_macro() { 43 | assert_eq!(eco_vec![Box::new(1); 3], vec![v(1); 3]); 44 | } 45 | 46 | #[test] 47 | fn test_vec_construction() { 48 | assert_eq!(EcoVec::<()>::default(), &[]); 49 | assert_eq!(EcoVec::from(vec![(); 100]), vec![(); 100]); 50 | } 51 | 52 | #[test] 53 | fn test_vec_with_capacity() { 54 | let mut vec = EcoVec::with_capacity(3); 55 | assert_eq!(vec.capacity(), 3); 56 | let ptr = vec.as_ptr(); 57 | vec.push(1); 58 | vec.push(2); 59 | vec.push(3); 60 | assert_eq!(ptr, vec.as_ptr()); 61 | vec.push(4); 62 | assert_eq!(vec, [1, 2, 3, 4]); 63 | } 64 | 65 | #[test] 66 | #[should_panic(expected = "capacity overflow")] 67 | fn test_vec_with_capacity_fail() { 68 | EcoVec::::with_capacity(usize::MAX); 69 | } 70 | 71 | #[test] 72 | fn test_vec_empty() { 73 | let mut first = EcoVec::with_capacity(3); 74 | assert!(first.is_empty()); 75 | assert_eq!(first.len(), 0); 76 | first.push("hi".to_string()); 77 | assert!(!first.is_empty()); 78 | assert_eq!(first.len(), 1); 79 | let second = first.clone(); 80 | first.clear(); 81 | assert!(first.is_empty()); 82 | first.clear(); 83 | assert!(first.is_empty()); 84 | assert_eq!(second.len(), 1); 85 | assert_eq!(second, ["hi".to_string()]); 86 | } 87 | 88 | #[test] 89 | fn test_vec_make_mut() { 90 | let mut first = eco_vec![4, -3, 11, 6, 10]; 91 | let ptr = first.as_ptr(); 92 | first.make_mut()[1] -= 1; 93 | assert_eq!(ptr, first.as_ptr()); 94 | let second = first.clone(); 95 | first.make_mut().sort(); 96 | assert_eq!(first, [-4, 4, 6, 10, 11]); 97 | assert_eq!(second, [4, -4, 11, 6, 10]); 98 | } 99 | 100 | #[test] 101 | fn test_vec_push() { 102 | let mut first = EcoVec::new(); 103 | first.push(1); 104 | first.push(2); 105 | first.push(3); 106 | assert_eq!(first.len(), 3); 107 | let mut second = first.clone(); 108 | let third = second.clone(); 109 | let _ = third.clone(); 110 | second.push(4); 111 | assert_eq!(second.len(), 4); 112 | assert_eq!(first, [1, 2, 3]); 113 | assert_eq!(second, [1, 2, 3, 4]); 114 | assert_eq!(third, [1, 2, 3]); 115 | assert_ne!(first.as_ptr(), second.as_ptr()); 116 | assert_eq!(first.as_ptr(), third.as_ptr()); 117 | } 118 | 119 | #[test] 120 | fn test_vec_pop() { 121 | let mut first = EcoVec::new(); 122 | assert_eq!(first.pop(), None); 123 | first.push(v("a")); 124 | let ptr = first.as_ptr(); 125 | assert_eq!(first.pop(), Some(v("a"))); 126 | first.push(v("b")); 127 | assert_eq!(ptr, first.as_ptr()); 128 | let second = first.clone(); 129 | assert_eq!(first[0], v("b")); 130 | assert_eq!(ptr, first.as_ptr()); 131 | assert_eq!(first.pop(), Some(v("b"))); 132 | assert_eq!(first, []); 133 | assert_eq!(second, [v("b")]); 134 | } 135 | 136 | #[test] 137 | fn test_vec_insert() { 138 | let mut first = EcoVec::new(); 139 | first.insert(0, "okay"); 140 | let ptr = first.as_ptr(); 141 | first.insert(0, "reverse"); 142 | let mut second = first.clone(); 143 | first.insert(2, "a"); 144 | first.insert(1, "b"); 145 | second.insert(2, "last"); 146 | assert_eq!(first, ["reverse", "b", "okay", "a"]); 147 | assert_eq!(second, ["reverse", "okay", "last"]); 148 | assert_ne!(ptr, first.as_ptr()); 149 | assert_eq!(ptr, second.as_ptr()); 150 | } 151 | 152 | #[test] 153 | #[should_panic(expected = "index is out bounds (index: 4, len: 3)")] 154 | fn test_vec_insert_fail() { 155 | EcoVec::from([1, 2, 3]).insert(4, 0); 156 | } 157 | 158 | #[test] 159 | fn test_vec_remove() { 160 | let mut first = EcoVec::with_capacity(4); 161 | let ptr = first.as_ptr(); 162 | first.extend_from_slice(&[v(2), v(4), v(1)]); 163 | let second = first.clone(); 164 | assert_eq!(first.remove(1), v(4)); 165 | assert_eq!(first, [v(2), v(1)]); 166 | assert_eq!(second, [v(2), v(4), v(1)]); 167 | assert_ne!(ptr, first.as_ptr()); 168 | assert_eq!(ptr, second.as_ptr()); 169 | } 170 | 171 | #[test] 172 | #[should_panic(expected = "index is out bounds (index: 4, len: 3)")] 173 | fn test_vec_remove_fail() { 174 | EcoVec::from([1, 2, 3]).remove(4); 175 | } 176 | 177 | #[test] 178 | fn test_vec_truncate() { 179 | let mut vec = eco_vec!["ok"; 10]; 180 | vec.truncate(13); 181 | vec.truncate(3); 182 | assert_eq!(vec, ["ok"; 3]); 183 | 184 | let mut cloned = vec.clone(); 185 | cloned.truncate(2); 186 | assert_eq!(cloned, ["ok"; 2]); 187 | } 188 | 189 | #[test] 190 | fn test_vec_extend() { 191 | let mut vec = EcoVec::new(); 192 | vec.extend_from_slice(&[]); 193 | vec.extend_from_slice(&[2, 3, 4]); 194 | assert_eq!(vec, [2, 3, 4]); 195 | } 196 | 197 | #[test] 198 | fn test_vec_into_iter() { 199 | let first = eco_vec![v(2), v(4), v(5)]; 200 | let mut second = first.clone(); 201 | assert_eq!(first.clone().into_iter().count(), 3); 202 | assert_eq!(second.clone().into_iter().rev().collect::>(), [v(5), v(4), v(2)]); 203 | second.clear(); 204 | assert_eq!(second.into_iter().collect::>(), []); 205 | assert_eq!(first.clone().into_iter().collect::>(), [v(2), v(4), v(5)]); 206 | let mut iter = first.into_iter(); 207 | assert_eq!(iter.len(), 3); 208 | assert_eq!(iter.next(), Some(v(2))); 209 | assert_eq!(iter.next_back(), Some(v(5))); 210 | assert_eq!(iter.as_slice(), [v(4)]); 211 | drop(iter); 212 | } 213 | 214 | #[test] 215 | fn test_vec_zst() { 216 | static COUNTER: AtomicUsize = AtomicUsize::new(0); 217 | 218 | #[derive(Clone)] 219 | struct Potato; 220 | impl Drop for Potato { 221 | fn drop(&mut self) { 222 | COUNTER.fetch_add(1, SeqCst); 223 | } 224 | } 225 | 226 | let mut vec = EcoVec::new(); 227 | for _ in 0..1000 { 228 | vec.push(Potato); 229 | } 230 | assert_eq!(vec.len(), 1000); 231 | drop(vec); 232 | 233 | assert_eq!(COUNTER.load(SeqCst), 1000); 234 | } 235 | 236 | #[test] 237 | fn test_vec_huge_alignment() { 238 | #[derive(Debug, PartialEq, Clone)] 239 | #[repr(align(128))] 240 | struct B(&'static str); 241 | let mut vec: EcoVec = 242 | "hello, world! what's going on?".split_whitespace().map(B).collect(); 243 | 244 | assert_eq!(vec.len(), 5); 245 | assert_eq!(vec.capacity(), 8); 246 | assert_eq!(vec, [B("hello,"), B("world!"), B("what's"), B("going"), B("on?")]); 247 | assert_eq!(vec.pop(), Some(B("on?"))); 248 | assert_eq!(vec.len(), 4); 249 | assert_eq!(vec.last(), Some(&B("going"))); 250 | assert_eq!(vec.remove(1), B("world!")); 251 | assert_eq!(vec.len(), 3); 252 | assert_eq!(vec, [B("hello,"), B("what's"), B("going")]); 253 | assert_eq!(vec[1], B("what's")); 254 | vec.push(B("where?")); 255 | vec.insert(1, B("wonder!")); 256 | assert_eq!(vec, [B("hello,"), B("wonder!"), B("what's"), B("going"), B("where?")]); 257 | vec.retain(|b| b.0.starts_with('w')); 258 | assert_eq!(vec, [B("wonder!"), B("what's"), B("where?")]); 259 | vec.truncate(1); 260 | assert_eq!(vec.last(), vec.first()); 261 | 262 | let empty: EcoVec = EcoVec::new(); 263 | assert_eq!(empty, &[]); 264 | } 265 | 266 | #[test] 267 | #[should_panic(expected = "dropped the hot potato!")] 268 | #[allow(unused_must_use)] 269 | fn test_vec_drop_panic() { 270 | #[derive(Clone)] 271 | struct Potato; 272 | impl Drop for Potato { 273 | fn drop(&mut self) { 274 | panic!("dropped the hot potato!"); 275 | } 276 | } 277 | 278 | eco_vec![Potato]; 279 | } 280 | 281 | #[test] 282 | #[should_panic(expected = "dropped the hot potato!")] 283 | fn test_vec_clear_drop_panic() { 284 | #[derive(Clone)] 285 | struct Potato; 286 | impl Drop for Potato { 287 | fn drop(&mut self) { 288 | panic!("dropped the hot potato!"); 289 | } 290 | } 291 | 292 | let mut vec = eco_vec![Potato]; 293 | vec.clear(); 294 | } 295 | 296 | #[test] 297 | fn test_array_from_vec() { 298 | let array = [String::from("foo"), String::from("bar")]; 299 | let a = EcoVec::from(array.clone()); 300 | let b = a.clone(); 301 | let c: [String; 2] = a.try_into().unwrap(); 302 | assert_eq!(b, c); 303 | let d = b.clone(); 304 | assert_eq!(c, array); 305 | assert_eq!(d, array); 306 | drop(b); 307 | assert_eq!(c, array); 308 | drop(d); 309 | assert_eq!(c, array); 310 | 311 | assert_eq!(<[String; 0]>::try_from(EcoVec::new()).unwrap(), <[String; 0]>::default()); 312 | } 313 | 314 | #[test] 315 | fn test_str_new() { 316 | // Test inline strings. 317 | assert_eq!(EcoString::new(), ""); 318 | assert_eq!(EcoString::from('a'), "a"); 319 | assert_eq!(EcoString::from('😀'), "😀"); 320 | assert_eq!(EcoString::from("abc"), "abc"); 321 | 322 | // Test around the inline limit. 323 | assert_eq!(EcoString::from(&ALPH[..LIMIT - 1]), ALPH[..LIMIT - 1]); 324 | assert_eq!(EcoString::from(&ALPH[..LIMIT]), ALPH[..LIMIT]); 325 | assert_eq!(EcoString::from(&ALPH[..LIMIT + 1]), ALPH[..LIMIT + 1]); 326 | 327 | // Test heap string. 328 | assert_eq!(EcoString::from(ALPH), ALPH); 329 | } 330 | 331 | #[test] 332 | fn test_str_push() { 333 | let mut v = EcoString::new(); 334 | v.push('a'); 335 | v.push('b'); 336 | v.push_str("cd😀"); 337 | assert_eq!(v, "abcd😀"); 338 | assert_eq!(v.len(), 8); 339 | 340 | // Test fully filling the inline storage. 341 | v.push_str("efghijk"); 342 | assert_eq!(v.len(), 15); 343 | 344 | // Test spilling with `push`. 345 | let mut a = v.clone(); 346 | assert_eq!(a, "abcd😀efghijk"); 347 | a.push('l'); 348 | assert_eq!(a, "abcd😀efghijkl"); 349 | assert_eq!(a.len(), 16); 350 | 351 | // Test spilling with `push_str`. 352 | let mut b = v.clone(); 353 | b.push_str("lmn"); 354 | assert_eq!(b, "abcd😀efghijklmn"); 355 | assert_eq!(b.len(), 18); 356 | 357 | // v should be unchanged. 358 | assert_eq!(v.len(), 15); 359 | } 360 | 361 | #[test] 362 | fn test_str_pop() { 363 | // Test with inline string. 364 | let mut v = EcoString::from("Hello World!"); 365 | assert_eq!(v.pop(), Some('!')); 366 | assert_eq!(v, "Hello World"); 367 | 368 | // Remove one-by-one. 369 | for _ in 0..10 { 370 | v.pop(); 371 | } 372 | 373 | assert_eq!(v, "H"); 374 | assert_eq!(v.pop(), Some('H')); 375 | assert_eq!(v, ""); 376 | assert!(v.is_empty()); 377 | 378 | // Test with large string. 379 | let mut v = EcoString::from(ALPH); 380 | assert_eq!(v.pop(), Some('z')); 381 | assert_eq!(v.len(), 25); 382 | } 383 | 384 | #[test] 385 | fn test_str_index() { 386 | // Test that we can use the index syntax. 387 | let v = EcoString::from("abc"); 388 | assert_eq!(&v[..2], "ab"); 389 | } 390 | 391 | #[test] 392 | fn test_str_case() { 393 | assert_eq!(EcoString::new().to_uppercase(), ""); 394 | assert_eq!(EcoString::from("abc").to_uppercase(), "ABC"); 395 | assert_eq!(EcoString::from("AΣ").to_lowercase(), "aς"); 396 | assert_eq!( 397 | EcoString::from("a").repeat(100).to_uppercase(), 398 | EcoString::from("A").repeat(100) 399 | ); 400 | assert_eq!( 401 | EcoString::from("Ö").repeat(20).to_lowercase(), 402 | EcoString::from("ö").repeat(20) 403 | ); 404 | } 405 | 406 | #[test] 407 | fn test_str_repeat() { 408 | // Test with empty string. 409 | assert_eq!(EcoString::new().repeat(0), ""); 410 | assert_eq!(EcoString::new().repeat(100), ""); 411 | 412 | // Test non-spilling and spilling case. 413 | let v = EcoString::from("abc"); 414 | assert_eq!(v.repeat(0), ""); 415 | assert_eq!(v.repeat(3), "abcabcabc"); 416 | assert_eq!(v.repeat(5), "abcabcabcabcabc"); 417 | } 418 | 419 | #[test] 420 | fn test_str_inline_okay() { 421 | assert_eq!(EcoString::inline("hello"), "hello"); 422 | } 423 | 424 | #[test] 425 | #[should_panic(expected = "exceeded inline capacity")] 426 | fn test_str_inline_capacity_exceeded() { 427 | EcoString::inline("this is a pretty long string"); 428 | } 429 | 430 | #[test] 431 | fn test_str_clear() { 432 | let mut inline_clear = EcoString::from("foo"); 433 | inline_clear.clear(); 434 | assert_eq!(inline_clear, ""); 435 | 436 | let mut spilled_clear: EcoString = std::iter::repeat('a').take(100).collect(); 437 | let cloned = spilled_clear.clone(); 438 | spilled_clear.clear(); 439 | assert_eq!(spilled_clear, ""); 440 | assert_eq!(cloned.len(), 100); 441 | } 442 | 443 | #[test] 444 | fn test_str_construction() { 445 | let from_cow = EcoString::from(Cow::Borrowed("foo")); 446 | let from_char_iter: EcoString = "foo".chars().collect(); 447 | let from_eco_string_iter: EcoString = 448 | [EcoString::from("f"), EcoString::from("oo")].into_iter().collect(); 449 | 450 | assert_eq!(from_cow, from_char_iter); 451 | assert_eq!(from_char_iter, from_eco_string_iter); 452 | assert_eq!(from_eco_string_iter, "foo"); 453 | 454 | let str_from_eco_string_ref = String::from(&from_cow); 455 | let str_from_eco_string = String::from(from_cow); 456 | 457 | assert_eq!(str_from_eco_string, str_from_eco_string_ref); 458 | assert_eq!(str_from_eco_string_ref, "foo"); 459 | } 460 | 461 | #[test] 462 | fn test_str_extend() { 463 | let mut s = EcoString::from("Hello, "); 464 | s.extend("world!".chars()); 465 | 466 | assert_eq!(s, "Hello, world!"); 467 | } 468 | 469 | #[test] 470 | fn test_str_add() { 471 | let hello = EcoString::from("Hello, "); 472 | let world = EcoString::from("world!"); 473 | 474 | let add = hello.clone() + world.clone(); 475 | let mut add_assign = hello.clone(); 476 | add_assign += world; 477 | 478 | assert_eq!(add, add_assign); 479 | assert_eq!(add, "Hello, world!"); 480 | 481 | let add_str = hello.clone() + "world!"; 482 | let mut add_assign_str = hello.clone(); 483 | add_assign_str += "world!"; 484 | 485 | assert_eq!(add_str, add_assign_str); 486 | assert_eq!(add_str, "Hello, world!"); 487 | } 488 | 489 | #[test] 490 | fn test_str_complex() { 491 | let mut foo = EcoString::default(); 492 | foo.write_char('f').unwrap(); 493 | foo.write_str("oo").unwrap(); 494 | 495 | let bar = EcoString::from("bar"); 496 | 497 | let mut hash_map: HashMap<_, EcoVec<_>> = [ 498 | (foo.clone(), eco_vec![foo.clone(), bar.clone(), foo]), 499 | (bar.clone(), eco_vec![bar; 1]), 500 | ] 501 | .into_iter() 502 | .collect(); 503 | 504 | hash_map.get_mut("foo").unwrap().make_mut().sort(); 505 | 506 | assert_eq!(hash_map.get("foo").unwrap(), &["bar".into(), "foo".into(), "foo".into()]); 507 | assert_eq!(hash_map.get("bar").unwrap(), &["bar".into()]); 508 | } 509 | -------------------------------------------------------------------------------- /tests/ub_guards.rs: -------------------------------------------------------------------------------- 1 | use ecow::{eco_vec, EcoVec}; 2 | 3 | // Guarding against something like: 4 | // https://github.com/servo/rust-smallvec/issues/96 aka RUSTSEC-2018-0003 5 | // If length isn't updated defensively then a panic when iterating could 6 | // double-free a value. 7 | #[test] 8 | #[should_panic(expected = "Panic on next")] 9 | fn panicky_iterator_unwinds_correctly() { 10 | struct PanicIter; 11 | 12 | impl Iterator for PanicIter { 13 | type Item = u32; 14 | 15 | fn size_hint(&self) -> (usize, Option) { 16 | (1, None) 17 | } 18 | 19 | fn next(&mut self) -> Option { 20 | panic!("Panic on next"); 21 | } 22 | } 23 | 24 | let mut v = eco_vec![1, 2, 3]; 25 | v.extend(PanicIter); 26 | } 27 | 28 | // Guarding against something like: 29 | // https://github.com/servo/rust-smallvec/issues/252 aka RUSTSEC-2021-0003 30 | // size_hint should only be treated as a hint, nothing more. 31 | #[test] 32 | fn small_size_hint_is_fine() { 33 | let mut v = EcoVec::new(); 34 | v.push(123); 35 | 36 | let iter = (0u8..=255).filter(|n| n % 2 == 0); 37 | assert_eq!(iter.size_hint().0, 0); 38 | 39 | v.extend(iter); 40 | 41 | assert_eq!( 42 | v, 43 | core::iter::once(123) 44 | .chain((0u8..=255).filter(|n| n % 2 == 0)) 45 | .collect::>() 46 | ); 47 | } 48 | 49 | // Guarding against something like: 50 | // https://github.com/Alexhuszagh/rust-stackvector/issues/2 aka RUSTSEC-2021-0048 51 | // size_hint should only be treated as a hint, nothing more. 52 | #[test] 53 | fn wacky_size_hint_is_fine() { 54 | struct IncorrectIterator(core::iter::Take>); 55 | 56 | impl IncorrectIterator { 57 | pub fn new() -> Self { 58 | IncorrectIterator(core::iter::repeat(1).take(20)) 59 | } 60 | } 61 | 62 | impl Iterator for IncorrectIterator { 63 | type Item = u8; 64 | 65 | fn next(&mut self) -> Option { 66 | self.0.next() 67 | } 68 | 69 | fn size_hint(&self) -> (usize, Option) { 70 | (20, Some(0)) 71 | } 72 | } 73 | 74 | let mut v = EcoVec::new(); 75 | v.extend(IncorrectIterator::new()); 76 | 77 | assert_eq!(v, IncorrectIterator::new().collect::>()) 78 | } 79 | --------------------------------------------------------------------------------