elsa-1.7.0/.cargo_vcs_info.json0000644000000001360000000000100117650ustar { "git": { "sha1": "6399daf502cdf5a6c53526b997bc92085667b0c6" }, "path_in_vcs": "" }elsa-1.7.0/.github/workflows/rust.yml000064400000000000000000000005320072674642500157220ustar 00000000000000name: Rust on: push: branches: [ master ] pull_request: branches: [ master ] env: CARGO_TERM_COLOR: always jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Build run: cargo build --verbose --all-features - name: Run tests run: cargo test --verbose --all-features elsa-1.7.0/.gitignore000064400000000000000000000000360072674642500125740ustar 00000000000000/target **/*.rs.bk Cargo.lock elsa-1.7.0/.travis.yml000064400000000000000000000004320072674642500127150ustar 00000000000000language: rust rust: nightly os: - linux script: - cargo build - cargo test - cargo run --example fluentresource - cargo run --example arena - cargo run --example mutable_arena - cargo run --example sync - cargo run --example string_interner --features indexmap elsa-1.7.0/Cargo.lock0000644000000020030000000000100077330ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "elsa" version = "1.7.0" dependencies = [ "indexmap", "stable_deref_trait", ] [[package]] name = "hashbrown" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "indexmap" version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" dependencies = [ "autocfg", "hashbrown", ] [[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" elsa-1.7.0/Cargo.toml0000644000000022260000000000100077650ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "elsa" version = "1.7.0" authors = ["Manish Goregaokar "] description = "Append-only collections for Rust where borrows to entries can outlive insertions" documentation = "https://docs.rs/elsa/" keywords = ["data-structure", "map", "frozen", "cache", "arena"] categories = ["data-structures", "caching"] license = "MIT/Apache-2.0" repository = "https://github.com/manishearth/elsa" [package.metadata.docs.rs] features = ["indexmap"] [[example]] name = "string_interner" path = "examples/string_interner.rs" required-features = ["indexmap"] [dependencies.indexmap] version = "1.6" optional = true [dependencies.stable_deref_trait] version = "1.1.1" elsa-1.7.0/Cargo.toml.orig000064400000000000000000000012640072674642500134770ustar 00000000000000[package] name = "elsa" version = "1.7.0" authors = ["Manish Goregaokar "] edition = "2018" description = "Append-only collections for Rust where borrows to entries can outlive insertions" license = "MIT/Apache-2.0" documentation = "https://docs.rs/elsa/" repository = "https://github.com/manishearth/elsa" keywords = ["data-structure", "map", "frozen", "cache", "arena"] categories = ["data-structures", "caching"] [dependencies] stable_deref_trait = "1.1.1" indexmap = { version = "1.6", optional = true } [package.metadata.docs.rs] features = ["indexmap"] [[example]] name = "string_interner" path = "examples/string_interner.rs" required-features = ["indexmap"] elsa-1.7.0/LICENSE-APACHE000064400000000000000000000251370072674642500125410ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. elsa-1.7.0/LICENSE-MIT000064400000000000000000000020620072674642500122410ustar 00000000000000MIT License Copyright (c) 2019 Manish Goregaokar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. elsa-1.7.0/README.md000064400000000000000000000016710072674642500120710ustar 00000000000000## elsa [![Build Status](https://travis-ci.org/Manishearth/elsa.svg?branch=master)](https://travis-ci.org/Manishearth/elsa) [![Current Version](https://meritbadge.herokuapp.com/elsa)](https://crates.io/crates/elsa) [![License: MIT/Apache-2.0](https://img.shields.io/crates/l/elsa.svg)](#license) _🎵 Immutability never bothered me anyway 🎶_ This crate provides various "frozen" collections. These are append-only collections where references to entries can be held on to even across insertions. This is safe because these collections only support storing data that's present behind some indirection -- i.e. `String`, `Vec`, `Box`, etc, and they only yield references to the data behind the allocation (`&str`, `&[T]`, and `&T` respectively) The typical use case is having a global cache of strings or other data which the rest of the program borrows from. ### Running all examples ```bash cargo test --examples --features indexmap ``` elsa-1.7.0/examples/arena.rs000064400000000000000000000027340072674642500140650ustar 00000000000000use elsa::FrozenVec; fn main() { let arena = Arena::new(); let lonely = arena.add_thing("lonely", vec![]); let best_friend = arena.add_thing("best friend", vec![lonely]); let threes_a_crowd = arena.add_thing("threes a crowd", vec![lonely, best_friend]); let rando = arena.add_thing("rando", vec![]); let _facebook = arena.add_thing("facebook", vec![rando, threes_a_crowd, lonely, best_friend]); assert!(cmp_ref(lonely, best_friend.friends[0])); assert!(cmp_ref(best_friend, threes_a_crowd.friends[1])); arena.dump(); } struct Arena<'arena> { things: FrozenVec>>, } struct Thing<'arena> { pub friends: Vec>, pub name: &'static str, } type ThingRef<'arena> = &'arena Thing<'arena>; impl<'arena> Arena<'arena> { fn new() -> Arena<'arena> { Arena { things: FrozenVec::new(), } } fn add_thing( &'arena self, name: &'static str, friends: Vec>, ) -> ThingRef<'arena> { let idx = self.things.len(); self.things.push(Box::new(Thing { name, friends })); &self.things[idx] } fn dump(&'arena self) { for thing in &self.things { println!("friends of {}:", thing.name); for friend in &thing.friends { println!("\t{}", friend.name); } } } } fn cmp_ref(x: &T, y: &T) -> bool { x as *const T as usize == y as *const T as usize } elsa-1.7.0/examples/fluentresource.rs000064400000000000000000000025640072674642500160450ustar 00000000000000use elsa::FrozenMap; /// Stores some parsed AST representation of the file #[derive(Debug)] pub struct FluentResource<'mgr>(&'mgr str); impl<'mgr> FluentResource<'mgr> { pub fn new(s: &'mgr str) -> Self { // very simple parse step FluentResource(&s[0..1]) } } /// Stores loaded files and parsed ASTs /// /// Parsed ASTs are zero-copy and /// contain references to the files pub struct ResourceManager<'mgr> { strings: FrozenMap, resources: FrozenMap>>, } impl<'mgr> ResourceManager<'mgr> { pub fn new() -> Self { ResourceManager { strings: FrozenMap::new(), resources: FrozenMap::new(), } } pub fn get_resource(&'mgr self, path: &str) -> &'mgr FluentResource<'mgr> { let strings = &self.strings; if strings.get(path).is_some() { return self.resources.get(path).unwrap(); } else { // pretend to load a file let string = format!("file for {}", path); let val = self.strings.insert(path.to_string(), string); let res = FluentResource::new(val); self.resources.insert(path.to_string(), Box::new(res)) } } } fn main() { let manager = ResourceManager::new(); let resource = manager.get_resource("somefile.ftl"); println!("{:?}", resource); } elsa-1.7.0/examples/mutable_arena.rs000064400000000000000000000042130072674642500155700ustar 00000000000000use elsa::FrozenVec; fn main() { let arena = Arena::new(); let lonely = arena.add_person("lonely", vec![]); let best_friend = arena.add_person("best friend", vec![lonely]); let threes_a_crowd = arena.add_person("threes a crowd", vec![lonely, best_friend]); let rando = arena.add_person("rando", vec![]); let _everyone = arena.add_person( "follows everyone", vec![rando, threes_a_crowd, lonely, best_friend], ); arena.dump(); } struct Arena<'arena> { people: FrozenVec>>, } struct Person<'arena> { pub follows: FrozenVec>, pub reverse_follows: FrozenVec>, pub name: &'static str, } type PersonRef<'arena> = &'arena Person<'arena>; impl<'arena> Arena<'arena> { fn new() -> Arena<'arena> { Arena { people: FrozenVec::new(), } } fn add_person( &'arena self, name: &'static str, follows: Vec>, ) -> PersonRef<'arena> { let idx = self.people.len(); self.people.push(Box::new(Person { name, follows: follows.into(), reverse_follows: Default::default(), })); let me = &self.people[idx]; for friend in &me.follows { friend.reverse_follows.push(me) } me } fn dump(&'arena self) { for thing in &self.people { println!("{} network:", thing.name); println!("\tfollowing:"); for friend in &thing.follows { println!("\t\t{}", friend.name); } println!("\tfollowers:"); for friend in &thing.reverse_follows { println!("\t\t{}", friend.name); } } } } // Note that the following will cause the above code to stop compiling // since non-eyepatched custom destructors can potentially // read deallocated data. // // impl<'arena> Drop for Person<'arena> { // fn drop(&mut self) { // println!("goodbye {:?}", self.name); // for friend in &self.follows { // println!("\t\t{}", friend.name); // } // } // } elsa-1.7.0/examples/string_interner.rs000064400000000000000000000032310072674642500162040ustar 00000000000000use std::collections::BTreeSet; use std::convert::AsRef; use elsa::FrozenIndexSet; struct StringInterner { set: FrozenIndexSet, } impl StringInterner { fn new() -> Self { StringInterner { set: FrozenIndexSet::new(), } } fn get_or_intern(&self, value: T) -> usize where T: AsRef, { // TODO use Entry in case the standard Entry API gets improved // (here to avoid premature allocation or double lookup) self.set.insert_full(value.as_ref().to_string()).0 } fn get(&self, value: T) -> Option where T: AsRef, { self.set.get_full(value.as_ref()).map(|(i, _r)| i) } fn resolve(&self, index: usize) -> Option<&str> { self.set.get_index(index) } } fn main() { let interner = StringInterner::new(); let lonely = interner.get_or_intern("lonely"); let best_friend = interner.get_or_intern("best friend"); let threes_a_crowd = interner.get_or_intern("threes a crowd"); let rando = interner.get_or_intern("rando"); let _facebook = interner.get_or_intern("facebook"); let best_friend_2 = interner.get_or_intern("best friend"); let best_friend_3 = interner.get("best friend").unwrap(); let best_friend_ref = interner.resolve(best_friend).unwrap(); let mut set = BTreeSet::new(); set.insert(lonely); set.insert(best_friend); set.insert(threes_a_crowd); set.insert(rando); set.insert(best_friend_2); assert_eq!(set.len(), 4); assert_eq!(best_friend, best_friend_2); assert_eq!(best_friend_2, best_friend_3); assert_eq!(best_friend_ref, "best friend"); } elsa-1.7.0/examples/sync.rs000064400000000000000000000012450072674642500137470ustar 00000000000000use elsa::sync::*; use std::sync::Arc; use std::thread; use std::time::Duration; fn main() { let a = Arc::new(FrozenMap::new()); for i in 1..10 { let b = a.clone(); thread::spawn(move || { b.insert(i, i.to_string()); thread::sleep(Duration::from_millis(300)); loop { if let Some(opposite) = b.get(&(10 - i)) { assert!(opposite.parse::().unwrap() == 10 - i); break; } else { thread::sleep(Duration::from_millis(200)); } } }); } thread::sleep(Duration::from_millis(1000)); } elsa-1.7.0/src/index_map.rs000064400000000000000000000133160072674642500137120ustar 00000000000000use std::borrow::Borrow; use std::cell::{Cell, UnsafeCell}; use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::Index; use indexmap::IndexMap; use stable_deref_trait::StableDeref; /// Append-only version of `indexmap::IndexMap` where /// insertion does not require mutable access pub struct FrozenIndexMap { map: UnsafeCell>, /// Eq/Hash implementations can have side-effects, and using Rc it is possible /// for FrozenIndexMap::insert to be called on a key that itself contains the same /// `FrozenIndexMap`, whose `eq` implementation also calls FrozenIndexMap::insert /// /// We use this `in_use` flag to guard against any reentrancy. in_use: Cell, } // safety: UnsafeCell implies !Sync impl FrozenIndexMap { pub fn new() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } impl FrozenIndexMap { // these should never return &K or &V // these should never delete any entries pub fn insert(&self, k: K, v: V) -> &V::Target { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); &*(*map).entry(k).or_insert(v) }; self.in_use.set(false); ret } // these should never return &K or &V // these should never delete any entries pub fn insert_full(&self, k: K, v: V) -> (usize, &V::Target) { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); let entry = (*map).entry(k); let index = entry.index(); (index, &**entry.or_insert(v)) }; self.in_use.set(false); ret } /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` pub fn get(&self, k: &Q) -> Option<&V::Target> where K: Borrow, Q: Hash + Eq, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(|x| &**x) }; self.in_use.set(false); ret } pub fn get_index(&self, index: usize) -> Option<(&K::Target, &V::Target)> where K: StableDeref, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get_index(index).map(|(k, v)| (&**k, &**v)) }; self.in_use.set(false); ret } /// Applies a function to the owner of the value corresponding to the key (if any). /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a"))); /// assert_eq!(map.map_get(&2, Clone::clone), None); /// ``` pub fn map_get(&self, k: &Q, f: F) -> Option where K: Borrow, Q: Hash + Eq, F: FnOnce(&V) -> T, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(f) }; self.in_use.set(false); ret } pub fn into_map(self) -> IndexMap { self.map.into_inner() } /// Get mutable access to the underlying [`IndexMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. pub fn as_mut(&mut self) -> &mut IndexMap { unsafe { &mut *self.map.get() } } /// Returns true if the map contains no elements. pub fn is_empty(&self) -> bool { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).is_empty() }; self.in_use.set(false); ret } } impl From> for FrozenIndexMap { fn from(map: IndexMap) -> Self { Self { map: UnsafeCell::new(map), in_use: Cell::new(false), } } } impl Index for FrozenIndexMap { type Output = V::Target; fn index(&self, idx: K) -> &V::Target { self.get(&idx) .expect("attempted to index FrozenIndexMap with unknown key") } } impl FromIterator<(K, V)> for FrozenIndexMap { fn from_iter(iter: T) -> Self where T: IntoIterator, { let map: IndexMap<_, _, _> = iter.into_iter().collect(); map.into() } } impl Default for FrozenIndexMap { fn default() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } elsa-1.7.0/src/index_set.rs000064400000000000000000000122120072674642500137220ustar 00000000000000use std::borrow::Borrow; use std::cell::{Cell, UnsafeCell}; use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::Index; use indexmap::IndexSet; use stable_deref_trait::StableDeref; /// Append-only version of `indexmap::IndexSet` where /// insertion does not require mutable access pub struct FrozenIndexSet { set: UnsafeCell>, /// Eq/Hash implementations can have side-effects, and using Rc it is possible /// for FrozenIndexSet::insert to be called on a key that itself contains the same /// `FrozenIndexSet`, whose `eq` implementation also calls FrozenIndexSet::insert /// /// We use this `in_use` flag to guard against any reentrancy. in_use: Cell, } // safety: UnsafeCell implies !Sync impl FrozenIndexSet { pub fn new() -> Self { Self::from(IndexSet::new()) } } impl FrozenIndexSet { // these should never return &T // these should never delete any entries pub fn insert(&self, value: T) -> &T::Target { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); let (index, _was_vacant) = (*set).insert_full(value); &*(*set)[index] }; self.in_use.set(false); ret } // these should never return &T // these should never delete any entries pub fn insert_full(&self, value: T) -> (usize, &T::Target) { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); let (index, _was_vacant) = (*set).insert_full(value); (index, &*(*set)[index]) }; self.in_use.set(false); ret } // TODO implement in case the standard Entry API gets improved // // TODO avoid double lookup // pub fn entry(&self, value: &Q) -> Entry // where Q: Hash + Equivalent + ToOwned // { // assert!(!self.in_use.get()); // self.in_use.set(true); // unsafe { // let set = self.set.get(); // match (*set).get_full(value) { // Some((index, reference)) => { // Entry::Occupied(OccupiedEntry { // index, // reference, // set: &*set, // }) // } // None => { // Entry::Vacant(VacantEntry { // value: Cow::Borrowed(value), // set: &*set, // }) // } // } // } // } pub fn get(&self, k: &Q) -> Option<&T::Target> where T: Borrow, Q: Hash + Eq, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); (*set).get(k).map(|x| &**x) }; self.in_use.set(false); ret } pub fn get_full(&self, k: &Q) -> Option<(usize, &T::Target)> where T: Borrow, Q: Hash + Eq, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); (*set).get_full(k).map(|(i, x)| (i, &**x)) }; self.in_use.set(false); ret } pub fn get_index(&self, index: usize) -> Option<&T::Target> { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); (*set).get_index(index).map(|r| &**r) }; self.in_use.set(false); ret } pub fn into_set(self) -> IndexSet { self.set.into_inner() } /// Get mutable access to the underlying [`IndexSet`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. pub fn as_mut(&mut self) -> &mut IndexSet { unsafe { &mut *self.set.get() } } // TODO add more } impl From> for FrozenIndexSet { fn from(set: IndexSet) -> Self { Self { set: UnsafeCell::new(set), in_use: Cell::new(false), } } } impl Index for FrozenIndexSet { type Output = T::Target; fn index(&self, idx: usize) -> &T::Target { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let set = self.set.get(); &*(*set)[idx] }; self.in_use.set(false); ret } } impl FromIterator for FrozenIndexSet { fn from_iter(iter: U) -> Self where U: IntoIterator, { let set: IndexSet<_, _> = iter.into_iter().collect(); set.into() } } impl Default for FrozenIndexSet { fn default() -> Self { Self::from(IndexSet::default()) } } elsa-1.7.0/src/lib.rs000064400000000000000000000016660072674642500125210ustar 00000000000000//! _🎵 Immutability never bothered me anyway 🎶_ //! //! This crate provides various "Frozen" collections. //! //! These are append-only collections where references to entries can be held //! on to even across insertions. This is safe because these collections only //! support storing data that's present behind some indirection -- i.e. `String`, //! `Vec`, `Box`, etc, and they only yield references to the data behind the //! allocation (`&str`, `&[T]`, and `&T` respectively) //! //! The typical use case is having a global cache of strings or other data which the rest of the program borrows from. pub mod map; pub mod vec; #[cfg(feature = "indexmap")] pub mod index_map; #[cfg(feature = "indexmap")] pub mod index_set; pub mod sync; pub use map::{FrozenBTreeMap, FrozenMap}; pub use vec::FrozenVec; #[cfg(feature = "indexmap")] pub use index_map::FrozenIndexMap; #[cfg(feature = "indexmap")] pub use index_set::FrozenIndexSet; elsa-1.7.0/src/map.rs000064400000000000000000000215320072674642500125220ustar 00000000000000use std::borrow::Borrow; use std::cell::{Cell, UnsafeCell}; use std::collections::hash_map::RandomState; use std::collections::BTreeMap; use std::collections::HashMap; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::Index; use stable_deref_trait::StableDeref; /// Append-only version of `std::collections::HashMap` where /// insertion does not require mutable access pub struct FrozenMap { map: UnsafeCell>, /// Eq/Hash implementations can have side-effects, and using Rc it is possible /// for FrozenMap::insert to be called on a key that itself contains the same /// `FrozenMap`, whose `eq` implementation also calls FrozenMap::insert /// /// We use this `in_use` flag to guard against any reentrancy. in_use: Cell, } // safety: UnsafeCell implies !Sync impl FrozenMap { pub fn new() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } impl FrozenMap { // these should never return &K or &V // these should never delete any entries pub fn insert(&self, k: K, v: V) -> &V::Target { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); &*(*map).entry(k).or_insert(v) }; self.in_use.set(false); ret } /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` pub fn get(&self, k: &Q) -> Option<&V::Target> where K: Borrow, Q: Hash + Eq, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(|x| &**x) }; self.in_use.set(false); ret } /// Applies a function to the owner of the value corresponding to the key (if any). /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a"))); /// assert_eq!(map.map_get(&2, Clone::clone), None); /// ``` pub fn map_get(&self, k: &Q, f: F) -> Option where K: Borrow, Q: Hash + Eq, F: FnOnce(&V) -> T, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(f) }; self.in_use.set(false); ret } pub fn into_map(self) -> HashMap { self.map.into_inner() } /// Get mutable access to the underlying [`HashMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. pub fn as_mut(&mut self) -> &mut HashMap { unsafe { &mut *self.map.get() } } // TODO add more } impl From> for FrozenMap { fn from(map: HashMap) -> Self { Self { map: UnsafeCell::new(map), in_use: Cell::new(false), } } } impl Index for FrozenMap { type Output = V::Target; fn index(&self, idx: K) -> &V::Target { self.get(&idx) .expect("attempted to index FrozenMap with unknown key") } } impl FromIterator<(K, V)> for FrozenMap { fn from_iter(iter: T) -> Self where T: IntoIterator, { let map: HashMap<_, _, _> = iter.into_iter().collect(); map.into() } } impl Default for FrozenMap { fn default() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } /// Append-only version of `std::collections::BTreeMap` where /// insertion does not require mutable access pub struct FrozenBTreeMap { map: UnsafeCell>, /// Eq/Hash implementations can have side-effects, and using Rc it is possible /// for FrozenBTreeMap::insert to be called on a key that itself contains the same /// `FrozenBTreeMap`, whose `eq` implementation also calls FrozenBTreeMap::insert /// /// We use this `in_use` flag to guard against any reentrancy. in_use: Cell, } // safety: UnsafeCell implies !Sync impl FrozenBTreeMap { pub fn new() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } impl FrozenBTreeMap { // these should never return &K or &V // these should never delete any entries pub fn insert(&self, k: K, v: V) -> &V::Target { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); &*(*map).entry(k).or_insert(v) }; self.in_use.set(false); ret } /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` pub fn get(&self, k: &Q) -> Option<&V::Target> where K: Borrow, Q: Ord, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(|x| &**x) }; self.in_use.set(false); ret } /// Applies a function to the owner of the value corresponding to the key (if any). /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a"))); /// assert_eq!(map.map_get(&2, Clone::clone), None); /// ``` pub fn map_get(&self, k: &Q, f: F) -> Option where K: Borrow, Q: Ord, F: FnOnce(&V) -> T, { assert!(!self.in_use.get()); self.in_use.set(true); let ret = unsafe { let map = self.map.get(); (*map).get(k).map(f) }; self.in_use.set(false); ret } pub fn into_map(self) -> BTreeMap { self.map.into_inner() } /// Get mutable access to the underlying [`HashMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. pub fn as_mut(&mut self) -> &mut BTreeMap { unsafe { &mut *self.map.get() } } // TODO add more } impl From> for FrozenBTreeMap { fn from(map: BTreeMap) -> Self { Self { map: UnsafeCell::new(map), in_use: Cell::new(false), } } } impl Index for FrozenBTreeMap { type Output = V::Target; fn index(&self, idx: K) -> &V::Target { self.get(&idx) .expect("attempted to index FrozenBTreeMap with unknown key") } } impl FromIterator<(K, V)> for FrozenBTreeMap { fn from_iter(iter: T) -> Self where T: IntoIterator, { let map: BTreeMap<_, _> = iter.into_iter().collect(); map.into() } } impl Default for FrozenBTreeMap { fn default() -> Self { Self { map: UnsafeCell::new(Default::default()), in_use: Cell::new(false), } } } elsa-1.7.0/src/sync.rs000064400000000000000000000170660072674642500127300ustar 00000000000000//! **This module is experimental** //! //! This module provides threadsafe versions of FrozenMap and FrozenVec, //! ideal for use as a cache. //! //! These lock internally, however locks only last as long as the method calls //! use stable_deref_trait::StableDeref; use std::borrow::Borrow; use std::collections::BTreeMap; use std::collections::HashMap; use std::hash::Hash; use std::iter::{FromIterator, IntoIterator}; use std::ops::Index; use std::sync::RwLock; /// Append-only threadsafe version of `std::collections::HashMap` where /// insertion does not require mutable access pub struct FrozenMap { map: RwLock>, } impl FrozenMap { // these should never return &K or &V // these should never delete any entries pub fn new() -> Self { Self { map: RwLock::new(Default::default()), } } pub fn insert(&self, k: K, v: V) -> &V::Target { let mut map = self.map.write().unwrap(); let ret = unsafe { let inserted = &**map.entry(k).or_insert(v); &*(inserted as *const _) }; ret } /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` pub fn get(&self, k: &Q) -> Option<&V::Target> where K: Borrow, Q: Hash + Eq, { let map = self.map.read().unwrap(); let ret = unsafe { map.get(k).map(|x| &*(&**x as *const V::Target)) }; ret } /// Applies a function to the owner of the value corresponding to the key (if any). /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a"))); /// assert_eq!(map.map_get(&2, Clone::clone), None); /// ``` pub fn map_get(&self, k: &Q, f: F) -> Option where K: Borrow, Q: Hash + Eq, F: FnOnce(&V) -> T, { let map = self.map.read().unwrap(); let ret = map.get(k).map(f); ret } // TODO add more } /// Append-only threadsafe version of `std::vec::Vec` where /// insertion does not require mutable access pub struct FrozenVec { vec: RwLock>, } impl FrozenVec { pub fn new() -> Self { Self { vec: RwLock::new(Default::default()), } } // these should never return &T // these should never delete any entries pub fn push(&self, val: T) { let mut vec = self.vec.write().unwrap(); vec.push(val); } /// Push, immediately getting a reference to the element pub fn push_get(&self, val: T) -> &T::Target { let mut vec = self.vec.write().unwrap(); vec.push(val); unsafe { &*(&**vec.get_unchecked(vec.len() - 1) as *const T::Target) } } /// Push, immediately getting a an index of the element /// /// Index can then be used with the `get` method /// /// # Examples /// /// ``` /// use elsa::sync::FrozenVec; /// /// let map = FrozenVec::new(); /// let idx = map.push_get_index(String::from("a")); /// assert_eq!(map.get(idx), Some("a")); /// assert_eq!(idx, 0); /// assert_eq!(map.push_get_index(String::from("b")), 1); /// ``` pub fn push_get_index(&self, val: T) -> usize { let mut vec = self.vec.write().unwrap(); let index = vec.len(); vec.push(val); return index; } pub fn get(&self, index: usize) -> Option<&T::Target> { let vec = self.vec.read().unwrap(); unsafe { vec.get(index).map(|x| &*(&**x as *const T::Target)) } } // TODO add more } /// Append-only threadsafe version of `std::collections::BTreeMap` where /// insertion does not require mutable access #[derive(Debug)] pub struct FrozenBTreeMap(RwLock>); impl FrozenBTreeMap { pub fn new() -> Self { Self(RwLock::new(BTreeMap::new())) } // these should never return &K or &V // these should never delete any entries /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Ord`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` pub fn get(&self, k: &Q) -> Option<&V::Target> where K: Borrow, Q: Ord, { let map = self.0.read().unwrap(); let ret = unsafe { map.get(k).map(|x| &*(&**x as *const V::Target)) }; ret } /// Insert a new value into the map. Does nothing if the key is already occupied. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.get(&1), Some(&"a")); /// ``` pub fn insert(&self, k: K, v: V) -> &V::Target { let mut map = self.0.write().unwrap(); let ret = unsafe { let inserted = &**map.entry(k).or_insert(v); &*(inserted as *const _) }; ret } /// Applies a function to the owner of the value corresponding to the key (if any). /// /// The key may be any borrowed form of the map's key type, but /// [`Ord`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a"))); /// assert_eq!(map.map_get(&2, Clone::clone), None); /// ``` pub fn map_get(&self, k: &Q, f: F) -> Option where K: Borrow, Q: Ord, F: FnOnce(&V) -> T, { let map = self.0.read().unwrap(); let ret = map.get(k).map(f); ret } } impl From> for FrozenBTreeMap { fn from(map: BTreeMap) -> Self { Self(RwLock::new(map)) } } impl Index for FrozenBTreeMap { type Output = V::Target; fn index(&self, idx: K) -> &V::Target { self.get(&idx) .expect("attempted to index FrozenBTreeMap with unknown key") } } impl FromIterator<(K, V)> for FrozenBTreeMap { fn from_iter(iter: T) -> Self where T: IntoIterator, { let map: BTreeMap<_, _> = iter.into_iter().collect(); map.into() } } impl Default for FrozenBTreeMap { fn default() -> Self { Self::new() } } elsa-1.7.0/src/vec.rs000064400000000000000000000207310072674642500125220ustar 00000000000000use std::cell::UnsafeCell; use std::cmp::Ordering; use std::iter::FromIterator; use std::ops::Index; use stable_deref_trait::StableDeref; /// Append-only version of `std::vec::Vec` where /// insertion does not require mutable access pub struct FrozenVec { vec: UnsafeCell>, // XXXManishearth do we need a reentrancy guard here as well? // StableDeref may not guarantee that there are no side effects } // safety: UnsafeCell implies !Sync impl FrozenVec { /// Constructs a new, empty vector. pub fn new() -> Self { Self { vec: UnsafeCell::new(Default::default()), } } } impl FrozenVec { // these should never return &T // these should never delete any entries /// Appends an element to the back of the vector. pub fn push(&self, val: T) { unsafe { let vec = self.vec.get(); (*vec).push(val) } } /// Push, immediately getting a reference to the element pub fn push_get(&self, val: T) -> &T::Target { unsafe { let vec = self.vec.get(); (*vec).push(val); &*(&**(*vec).get_unchecked((*vec).len() - 1) as *const T::Target) } } /// Returns a reference to an element. pub fn get(&self, index: usize) -> Option<&T::Target> { unsafe { let vec = self.vec.get(); (*vec).get(index).map(|x| &**x) } } /// Returns a reference to an element, without doing bounds checking. /// /// ## Safety /// /// `index` must be in bounds, i.e. it must be less than `self.len()` pub unsafe fn get_unchecked(&self, index: usize) -> &T::Target { let vec = self.vec.get(); &**(*vec).get_unchecked(index) } /// Returns the number of elements in the vector. pub fn len(&self) -> usize { unsafe { let vec = self.vec.get(); (*vec).len() } } /// Returns `true` if the vector contains no elements. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns the first element of the vector, or `None` if empty. pub fn first(&self) -> Option<&T::Target> { unsafe { let vec = self.vec.get(); (*vec).first().map(|x| &**x) } } /// Returns the last element of the vector, or `None` if empty. pub fn last(&self) -> Option<&T::Target> { unsafe { let vec = self.vec.get(); (*vec).last().map(|x| &**x) } } /// Returns an iterator over the vector. pub fn iter(&self) -> Iter { self.into_iter() } /// Converts the frozen vector into a plain vector. pub fn into_vec(self) -> Vec { self.vec.into_inner() } /// Get mutable access to the underlying vector. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. pub fn as_mut(&mut self) -> &mut Vec { unsafe { &mut *self.vec.get() } } // binary search functions: they need to be reimplemented here to be safe (instead of calling // their equivalents directly on the underlying Vec), as they run user callbacks that could // reentrantly call other functions on this vector /// Binary searches this sorted vector for a given element, analogous to [slice::binary_search]. pub fn binary_search(&self, x: &T::Target) -> Result where T::Target: Ord, { self.binary_search_by(|p| p.cmp(x)) } /// Binary searches this sorted vector with a comparator function, analogous to /// [slice::binary_search_by]. pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result where F: FnMut(&'a T::Target) -> Ordering, { let mut size = self.len(); let mut left = 0; let mut right = size; while left < right { let mid = left + size / 2; // safety: like the core algorithm, mid is always within original vector len; in // pathlogical cases, user could push to the vector in the meantime, but this can only // increase the length, keeping this safe let cmp = f(unsafe { self.get_unchecked(mid) }); if cmp == Ordering::Less { left = mid + 1; } else if cmp == Ordering::Greater { right = mid; } else { return Ok(mid); } size = right - left; } Err(left) } /// Binary searches this sorted vector with a key extraction function, analogous to /// [slice::binary_search_by_key]. pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result where F: FnMut(&'a T::Target) -> B, B: Ord, { self.binary_search_by(|k| f(k).cmp(b)) } /// Returns the index of the partition point according to the given predicate /// (the index of the first element of the second partition), analogous to /// [slice::partition_point]. pub fn partition_point

(&self, mut pred: P) -> usize where P: FnMut(&T::Target) -> bool, { let mut left = 0; let mut right = self.len(); while left != right { let mid = left + (right - left) / 2; // safety: like in binary_search_by let value = unsafe { self.get_unchecked(mid) }; if pred(value) { left = mid + 1; } else { right = mid; } } left } // TODO add more } impl Default for FrozenVec { fn default() -> Self { FrozenVec::new() } } impl From> for FrozenVec { fn from(vec: Vec) -> Self { Self { vec: UnsafeCell::new(vec), } } } impl Index for FrozenVec { type Output = T::Target; fn index(&self, idx: usize) -> &T::Target { self.get(idx).unwrap_or_else(|| { panic!( "index out of bounds: the len is {} but the index is {}", self.len(), idx ) }) } } impl FromIterator for FrozenVec { fn from_iter(iter: T) -> Self where T: IntoIterator, { let vec: Vec<_> = iter.into_iter().collect(); vec.into() } } /// Iterator over FrozenVec, obtained via `.iter()` /// /// It is safe to push to the vector during iteration pub struct Iter<'a, T> { vec: &'a FrozenVec, idx: usize, } impl<'a, T: StableDeref> Iterator for Iter<'a, T> { type Item = &'a T::Target; fn next(&mut self) -> Option<&'a T::Target> { if let Some(ret) = self.vec.get(self.idx) { self.idx += 1; Some(ret) } else { None } } } impl<'a, T: StableDeref> IntoIterator for &'a FrozenVec { type Item = &'a T::Target; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { Iter { vec: self, idx: 0 } } } #[test] fn test_iteration() { let vec = vec!["a", "b", "c", "d"]; let frozen: FrozenVec<_> = vec.clone().into(); assert_eq!(vec, frozen.iter().collect::>()); for (e1, e2) in vec.iter().zip(frozen.iter()) { assert_eq!(*e1, e2); } assert_eq!(vec.len(), frozen.iter().count()) } #[test] fn test_accessors() { let vec: FrozenVec = FrozenVec::new(); assert_eq!(vec.is_empty(), true); assert_eq!(vec.len(), 0); assert_eq!(vec.first(), None); assert_eq!(vec.last(), None); assert_eq!(vec.get(1), None); vec.push("a".to_string()); vec.push("b".to_string()); vec.push("c".to_string()); assert_eq!(vec.is_empty(), false); assert_eq!(vec.len(), 3); assert_eq!(vec.first(), Some("a")); assert_eq!(vec.last(), Some("c")); assert_eq!(vec.get(1), Some("b")); } #[test] fn test_binary_search() { let vec: FrozenVec<_> = vec!["ab", "cde", "fghij"].into(); assert_eq!(vec.binary_search("cde"), Ok(1)); assert_eq!(vec.binary_search("cdf"), Err(2)); assert_eq!(vec.binary_search("a"), Err(0)); assert_eq!(vec.binary_search("g"), Err(3)); assert_eq!(vec.binary_search_by_key(&1, |x| x.len()), Err(0)); assert_eq!(vec.binary_search_by_key(&3, |x| x.len()), Ok(1)); assert_eq!(vec.binary_search_by_key(&4, |x| x.len()), Err(2)); assert_eq!(vec.partition_point(|x| x.len() < 4), 2); assert_eq!(vec.partition_point(|_| false), 0); assert_eq!(vec.partition_point(|_| true), 3); }