elsa-1.10.0/.cargo_vcs_info.json0000644000000001360000000000100120370ustar { "git": { "sha1": "5b7fa207fa235a3978dc4de59410ab675efa12eb" }, "path_in_vcs": "" }elsa-1.10.0/.github/workflows/rust.yml000064400000000000000000000005321046102023000157440ustar 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.10.0/.gitignore000064400000000000000000000000361046102023000126160ustar 00000000000000/target **/*.rs.bk Cargo.lock elsa-1.10.0/.travis.yml000064400000000000000000000004321046102023000127370ustar 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.10.0/Cargo.lock0000644000000020120000000000100100050ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "elsa" version = "1.10.0" dependencies = [ "indexmap", "stable_deref_trait", ] [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "indexmap" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", "hashbrown", ] [[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" elsa-1.10.0/Cargo.toml0000644000000024070000000000100100400ustar # 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.10.0" authors = ["Manish Goregaokar "] description = "Append-only collections for Rust where borrows to entries can outlive insertions" documentation = "https://docs.rs/elsa/" readme = "README.md" 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 = "2.0.2" optional = true [dependencies.stable_deref_trait] version = "1.1.1" [features] default = [] indexmap = ["dep:indexmap"] elsa-1.10.0/Cargo.toml.orig000064400000000000000000000013541046102023000135210ustar 00000000000000[package] name = "elsa" version = "1.10.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 = "2.0.2", optional = true } [package.metadata.docs.rs] features = ["indexmap"] [[example]] name = "string_interner" path = "examples/string_interner.rs" required-features = ["indexmap"] [features] default = [] indexmap = ["dep:indexmap"] elsa-1.10.0/LICENSE-APACHE000064400000000000000000000251371046102023000125630ustar 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.10.0/LICENSE-MIT000064400000000000000000000020621046102023000122630ustar 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.10.0/README.md000064400000000000000000000016741046102023000121160ustar 00000000000000## elsa [![Build Status](https://travis-ci.org/Manishearth/elsa.svg?branch=master)](https://travis-ci.org/Manishearth/elsa) [![Current Version](https://img.shields.io/crates/v/elsa.svg)](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.10.0/examples/arena.rs000064400000000000000000000027341046102023000141070ustar 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.10.0/examples/fluentresource.rs000064400000000000000000000025641046102023000160670ustar 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.10.0/examples/mutable_arena.rs000064400000000000000000000042131046102023000156120ustar 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.10.0/examples/string_interner.rs000064400000000000000000000032311046102023000162260ustar 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.10.0/examples/sync.rs000064400000000000000000000012451046102023000137710ustar 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.10.0/src/index_map.rs000064400000000000000000000226661046102023000137440ustar 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 // /// If the key exists in the map, returns a reference to the corresponding /// value, otherwise inserts a new entry in the map for that key /// and returns a reference to the generated value. /// /// Existing values are never overwritten. /// /// 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. /// /// # Example /// ``` /// use elsa::index_map::FrozenIndexMap; /// let map = FrozenIndexMap::new(); /// assert_eq!(map.insert(1, Box::new("a")), &"a"); /// assert_eq!(map.insert(1, Box::new("b")), &"a"); /// ``` 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 // /// If the key exists in the map, returns a reference to the corresponding /// value and its index, otherwise inserts a new entry in the map for that key /// and returns a reference to the generated value and its index. /// /// Existing values are never overwritten. /// /// 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. /// /// # Example /// ``` /// use elsa::index_map::FrozenIndexMap; /// let map = FrozenIndexMap::new(); /// assert_eq!(map.insert_full(12, Box::new("a")), (0, &"a")); /// assert_eq!(map.insert_full(12, Box::new("b")), (0, &"a")); /// ``` 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 } /// Returns a reference to the key-value mapping corresponding to an index. /// /// 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::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// let (idx, _ref) = map.insert_full(Box::new("foo"), Box::new("a")); /// assert_eq!(idx, 0); /// assert_eq!(map.get_index(idx), Some((&"foo", &"a"))); /// assert_eq!(map.get_index(idx + 1), None); /// ``` 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 } } impl FrozenIndexMap { /// Collects the contents of this map into a vector of tuples. /// /// The order of the entries is as if iterating an [`IndexMap`]. /// /// # Examples /// /// ``` /// use elsa::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); /// map.insert(2, Box::new("b")); /// let tuple_vec = map.into_tuple_vec(); /// /// assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]); /// ``` pub fn into_tuple_vec(self) -> Vec<(K, V)> { self.map.into_inner().into_iter().collect::>() } 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<&Q> for FrozenIndexMap where Q: Eq + Hash, K: Eq + Hash + Borrow, V: StableDeref, S: BuildHasher, { type Output = V::Target; /// # Examples /// /// ``` /// use elsa::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map[&1], "a"); /// ``` fn index(&self, idx: &Q) -> &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), } } } impl Clone for FrozenIndexMap { fn clone(&self) -> Self { assert!(!self.in_use.get()); self.in_use.set(true); let self_clone = Self { map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(), in_use: Cell::from(false), }; self.in_use.set(false); return self_clone; } } impl PartialEq for FrozenIndexMap { fn eq(&self, other: &Self) -> bool { assert!(!self.in_use.get()); assert!(!other.in_use.get()); self.in_use.set(true); other.in_use.set(true); let ret = unsafe { self.map.get().as_ref() == other.map.get().as_ref() }; self.in_use.set(false); other.in_use.set(false); ret } } elsa-1.10.0/src/index_set.rs000064400000000000000000000204441046102023000137520ustar 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 // /// If the value exists in the set, returns a reference to the corresponding /// value, otherwise inserts a new entry in the set for that value /// and returns a reference to it. /// /// Existing values are never overwritten. /// /// # Example /// ``` /// use elsa::index_set::FrozenIndexSet; /// let set = FrozenIndexSet::new(); /// let a_ref = set.insert(Box::new("a")); /// let aa = "a"; /// let other_a_ref = unsafe { aa.as_ptr() as *const &str}; /// let other_a = Box::new(aa); /// assert!(!std::ptr::eq(a_ref, other_a_ref)); /// assert!(std::ptr::eq(a_ref, set.insert(other_a))); /// ``` 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 /// If the key exists in the set, returns a reference to the corresponding /// value and its index, otherwise inserts a new entry in the set for that value /// and returns a reference to it and its index. /// /// Existing values are never overwritten. /// /// # Example /// ``` /// use elsa::index_set::FrozenIndexSet; /// let map = FrozenIndexSet::new(); /// assert_eq!(map.insert_full(Box::new("a")), (0, &"a")); /// assert_eq!(map.insert_full(Box::new("b")), (1, &"b")); /// ``` 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, // }) // } // } // } // } /// Returns a reference to the value passed as argument if present in the set. /// /// # Examples /// /// ``` /// use elsa::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); /// assert_eq!(set.get(&Box::new("a")), Some(&"a")); /// assert_eq!(set.get(&Box::new("b")), None); /// ``` 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 } /// Returns a reference to the value passed as argument if present in the set, /// along with its index /// /// # Examples /// /// ``` /// use elsa::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); /// assert_eq!(set.get_full(&Box::new("a")), Some((0, &"a"))); /// assert_eq!(set.get_full(&Box::new("b")), None); /// ``` 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 } /// Returns a reference to value at the index passed as argument, if /// present in the set. /// /// # Examples /// /// ``` /// use elsa::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); /// assert_eq!(set.get_index(0), Some(&"a")); /// assert_eq!(set.get_index(1), None); /// ``` 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 } } impl FrozenIndexSet { 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()) } } impl Clone for FrozenIndexSet { fn clone(&self) -> Self { assert!(!self.in_use.get()); self.in_use.set(true); let self_clone = Self { set: unsafe { self.set.get().as_ref().unwrap() }.clone().into(), in_use: Cell::from(false), }; self.in_use.set(false); return self_clone; } } impl PartialEq for FrozenIndexSet { fn eq(&self, other: &Self) -> bool { assert!(!self.in_use.get()); assert!(!other.in_use.get()); self.in_use.set(true); other.in_use.set(true); let ret = unsafe { self.set.get().as_ref() == other.set.get().as_ref() }; self.in_use.set(false); other.in_use.set(false); ret } } elsa-1.10.0/src/lib.rs000064400000000000000000000016661046102023000125430ustar 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.10.0/src/map.rs000064400000000000000000000360441046102023000125500ustar 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), } } /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.len(), 0); /// map.insert(1, Box::new("a")); /// assert_eq!(map.len(), 1); /// ``` pub fn len(&self) -> usize { assert!(!self.in_use.get()); self.in_use.set(true); let len = unsafe { let map = self.map.get(); (*map).len() }; self.in_use.set(false); len } /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.is_empty(), true); /// map.insert(1, Box::new("a")); /// assert_eq!(map.is_empty(), false); /// ``` pub fn is_empty(&self) -> bool { self.len() == 0 } } 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 } } impl FrozenMap { /// Collects the contents of this map into a vector of tuples. /// /// The order of the entries is as if iterating a [`HashMap`] (stochastic). /// /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// map.insert(2, Box::new("b")); /// let mut tuple_vec = map.into_tuple_vec(); /// tuple_vec.sort(); /// /// assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]); /// ``` pub fn into_tuple_vec(self) -> Vec<(K, V)> { self.map.into_inner().into_iter().collect::>() } pub fn into_map(self) -> HashMap { self.map.into_inner() } // TODO add more } impl FrozenMap { /// Returns a reference to the key and value matching a borrowed /// key. /// /// The key argument 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(Box::new("1"), Box::new("a")); /// assert_eq!(map.get_key_value(&"1"), Some((&"1", &"a"))); /// assert_eq!(map.get_key_value(&"2"), None); /// ``` pub fn get_key_value(&self, k: &Q) -> Option<(&K::Target, &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_key_value(k).map(|(k, v)| (&**k, &**v)) }; self.in_use.set(false); ret } } impl std::convert::AsMut> for FrozenMap { /// Get mutable access to the underlying [`HashMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. fn as_mut(&mut self) -> &mut HashMap { unsafe { &mut *self.map.get() } } } impl From> for FrozenMap { fn from(map: HashMap) -> Self { Self { map: UnsafeCell::new(map), in_use: Cell::new(false), } } } impl Index<&Q> for FrozenMap where Q: Eq + Hash, K: Eq + Hash + Borrow, V: StableDeref, S: BuildHasher, { type Output = V::Target; /// # Examples /// /// ``` /// use elsa::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map[&1], "a"); /// ``` fn index(&self, idx: &Q) -> &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), } } } impl Clone for FrozenMap { fn clone(&self) -> Self { assert!(!self.in_use.get()); self.in_use.set(true); let self_clone = Self { map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(), in_use: Cell::from(false), }; self.in_use.set(false); return self_clone; } } impl PartialEq for FrozenMap { fn eq(&self, other: &Self) -> bool { assert!(!self.in_use.get()); assert!(!other.in_use.get()); self.in_use.set(true); other.in_use.set(true); let ret = unsafe { self.map.get().as_ref() == other.map.get().as_ref() }; self.in_use.set(false); other.in_use.set(false); ret } } /// 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), } } /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// assert_eq!(map.len(), 0); /// map.insert(1, Box::new("a")); /// assert_eq!(map.len(), 1); /// ``` pub fn len(&self) -> usize { assert!(!self.in_use.get()); self.in_use.set(true); let len = unsafe { let map = self.map.get(); (*map).len() }; self.in_use.set(false); len } /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// assert_eq!(map.is_empty(), true); /// map.insert(1, Box::new("a")); /// assert_eq!(map.is_empty(), false); /// ``` pub fn is_empty(&self) -> bool { self.len() == 0 } } 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 } } impl FrozenBTreeMap { /// Collects the contents of this map into a vector of tuples. /// /// The order of the entries is as if iterating a [`BTreeMap`] (ordered by key). /// /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// map.insert(2, Box::new("b")); /// let mut tuple_vec = map.into_tuple_vec(); /// tuple_vec.sort(); /// /// assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]); /// ``` pub fn into_tuple_vec(self) -> Vec<(K, V)> { self.map.into_inner().into_iter().collect::>() } pub fn into_map(self) -> BTreeMap { self.map.into_inner() } // TODO add more } impl std::convert::AsMut> for FrozenBTreeMap { /// Get mutable access to the underlying [`HashMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. fn as_mut(&mut self) -> &mut BTreeMap { unsafe { &mut *self.map.get() } } } impl From> for FrozenBTreeMap { fn from(map: BTreeMap) -> Self { Self { map: UnsafeCell::new(map), in_use: Cell::new(false), } } } impl Index<&Q> for FrozenBTreeMap where Q: Ord, K: Clone + Ord + Borrow, V: StableDeref, { type Output = V::Target; /// # Examples /// /// ``` /// use elsa::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map[&1], "a"); /// ``` fn index(&self, idx: &Q) -> &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), } } } impl Clone for FrozenBTreeMap { fn clone(&self) -> Self { assert!(!self.in_use.get()); self.in_use.set(true); let self_clone = Self { map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(), in_use: Cell::from(false), }; self.in_use.set(false); return self_clone; } } impl PartialEq for FrozenBTreeMap { fn eq(&self, other: &Self) -> bool { assert!(!self.in_use.get()); assert!(!other.in_use.get()); self.in_use.set(true); other.in_use.set(true); let ret = unsafe { self.map.get().as_ref() == other.map.get().as_ref() }; self.in_use.set(false); other.in_use.set(false); ret } } elsa-1.10.0/src/sync.rs000064400000000000000000001106171046102023000127460ustar 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::alloc::Layout; use std::borrow::Borrow; use std::cmp::Eq; use std::collections::BTreeMap; use std::collections::HashMap; use std::fmt; use std::hash::Hash; use std::iter::{FromIterator, IntoIterator}; use std::ops::Index; use std::ptr::NonNull; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicPtr; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::sync::RwLock; use std::sync::TryLockError; /// Append-only threadsafe version of `std::collections::HashMap` where /// insertion does not require mutable access pub struct FrozenMap { map: RwLock>, } impl fmt::Debug for FrozenMap { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.map.try_read() { Ok(guard) => guard.fmt(f), Err(TryLockError::Poisoned(err)) => { f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish() } Err(TryLockError::WouldBlock) => { struct LockedPlaceholder; impl fmt::Debug for LockedPlaceholder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("") } } f.debug_tuple("FrozenMap") .field(&LockedPlaceholder) .finish() } } } } impl Default for FrozenMap { fn default() -> Self { Self { map: Default::default(), } } } impl FrozenMap { pub fn new() -> Self { Self::default() } } impl From> for FrozenVec { fn from(vec: Vec) -> Self { Self { vec: RwLock::new(vec), } } } impl FrozenMap { // these should never return &K or &V // these should never delete any entries /// If the key exists in the map, returns a reference /// to the corresponding value, otherwise inserts a /// new entry in the map for that key and returns a /// reference to the given value. /// /// Existing values are never overwritten. /// /// 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(); /// assert_eq!(map.insert(1, Box::new("a")), &"a"); /// assert_eq!(map.insert(1, Box::new("b")), &"a"); /// ``` 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 } /// If the key exists in the map, returns a reference to the corresponding /// value, otherwise inserts a new entry in the map for that key and the /// value returned by the creation function, and returns a reference to the /// generated value. /// /// Existing values are never overwritten. /// /// 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. /// /// **Note** that the write lock is held for the duration of this function’s /// execution, even while the value creation function is executing (if /// needed). This will block any concurrent `get` or `insert` calls. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.insert_with(1, || Box::new("a")), &"a"); /// assert_eq!(map.insert_with(1, || unreachable!()), &"a"); /// ``` pub fn insert_with(&self, k: K, f: impl FnOnce() -> V) -> &V::Target { let mut map = self.map.write().unwrap(); let ret = unsafe { let inserted = &**map.entry(k).or_insert_with(f); &*(inserted as *const _) }; ret } /// If the key exists in the map, returns a reference to the corresponding /// value, otherwise inserts a new entry in the map for that key and the /// value returned by the creation function, and returns a reference to the /// generated value. /// /// Existing values are never overwritten. /// /// 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. /// /// **Note** that the write lock is held for the duration of this function’s /// execution, even while the value creation function is executing (if /// needed). This will block any concurrent `get` or `insert` calls. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.insert_with_key(1, |_| Box::new("a")), &"a"); /// assert_eq!(map.insert_with_key(1, |_| unreachable!()), &"a"); /// ``` pub fn insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> &V::Target { let mut map = self.map.write().unwrap(); let ret = unsafe { let inserted = &**map.entry(k).or_insert_with_key(f); &*(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 } } impl FrozenMap { /// Collects the contents of this map into a vector of tuples. /// /// The order of the entries is as if iterating a [`HashMap`] (stochastic). /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// map.insert(1, Box::new("a")); /// map.insert(2, Box::new("b")); /// let mut tuple_vec = map.into_tuple_vec(); /// tuple_vec.sort(); /// /// assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]); /// ``` pub fn into_tuple_vec(self) -> Vec<(K, V)> { self.map .into_inner() .unwrap() .into_iter() .collect::>() } /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.len(), 0); /// map.insert(1, Box::new("a")); /// assert_eq!(map.len(), 1); /// ``` pub fn len(&self) -> usize { let map = self.map.read().unwrap(); map.len() } /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.is_empty(), true); /// map.insert(1, Box::new("a")); /// assert_eq!(map.is_empty(), false); /// ``` pub fn is_empty(&self) -> bool { let map = self.map.read().unwrap(); map.is_empty() } // TODO add more } impl FrozenMap { pub fn keys_cloned(&self) -> Vec { self.map.read().unwrap().keys().cloned().collect() } } impl FrozenMap { /// Returns a copy of 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.get_copy_or_insert(1, 6); /// assert_eq!(map.get_copy(&1), Some(6)); /// assert_eq!(map.get_copy(&2), None); /// ``` pub fn get_copy(&self, k: &Q) -> Option where K: Borrow, Q: Hash + Eq, { let map = self.map.read().unwrap(); map.get(k).cloned() } /// If the key exists in the map, returns a reference /// to the corresponding value, otherwise inserts a /// new entry in the map for that key and returns a /// reference to the given value. /// /// Existing values are never overwritten. /// /// 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(); /// assert_eq!(map.get_copy_or_insert(1, 6), 6); /// assert_eq!(map.get_copy_or_insert(1, 12), 6); /// ``` pub fn get_copy_or_insert(&self, k: K, v: V) -> V { let mut map = self.map.write().unwrap(); // This is safe because `or_insert` does not overwrite existing values let inserted = map.entry(k).or_insert(v); *inserted } /// If the key exists in the map, returns a reference to the corresponding /// value, otherwise inserts a new entry in the map for that key and the /// value returned by the creation function, and returns a reference to the /// generated value. /// /// Existing values are never overwritten. /// /// 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. /// /// **Note** that the write lock is held for the duration of this function’s /// execution, even while the value creation function is executing (if /// needed). This will block any concurrent `get` or `insert` calls. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.get_copy_or_insert_with(1, || 6), 6); /// assert_eq!(map.get_copy_or_insert_with(1, || unreachable!()), 6); /// ``` pub fn get_copy_or_insert_with(&self, k: K, f: impl FnOnce() -> V) -> V { let mut map = self.map.write().unwrap(); // This is safe because `or_insert_with` does not overwrite existing values let inserted = map.entry(k).or_insert_with(f); *inserted } /// If the key exists in the map, returns a reference to the corresponding /// value, otherwise inserts a new entry in the map for that key and the /// value returned by the creation function, and returns a reference to the /// generated value. /// /// Existing values are never overwritten. /// /// 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. /// /// **Note** that the write lock is held for the duration of this function’s /// execution, even while the value creation function is executing (if /// needed). This will block any concurrent `get` or `insert` calls. /// /// # Examples /// /// ``` /// use elsa::sync::FrozenMap; /// /// let map = FrozenMap::new(); /// assert_eq!(map.get_copy_or_insert_with_key(1, |_| 6), 6); /// assert_eq!(map.get_copy_or_insert_with_key(1, |_| unreachable!()), 6); /// ``` pub fn get_copy_or_insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> V { let mut map = self.map.write().unwrap(); // This is safe because `or_insert_with_key` does not overwrite existing values let inserted = map.entry(k).or_insert_with_key(f); *inserted } } impl std::convert::AsMut> for FrozenMap { /// Get mutable access to the underlying [`HashMap`]. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. fn as_mut(&mut self) -> &mut HashMap { self.map.get_mut().unwrap() } } impl Clone for FrozenMap { fn clone(&self) -> Self { Self { map: self.map.read().unwrap().clone().into(), } } } impl PartialEq for FrozenMap { fn eq(&self, other: &Self) -> bool { let self_ref: &HashMap = &self.map.read().unwrap(); let other_ref: &HashMap = &other.map.read().unwrap(); self_ref == other_ref } } /// Append-only threadsafe version of `std::vec::Vec` where /// insertion does not require mutable access pub struct FrozenVec { vec: RwLock>, } impl fmt::Debug for FrozenVec { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.vec.try_read() { Ok(guard) => guard.fmt(f), Err(TryLockError::Poisoned(err)) => { f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish() } Err(TryLockError::WouldBlock) => { struct LockedPlaceholder; impl fmt::Debug for LockedPlaceholder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("") } } f.debug_tuple("FrozenMap") .field(&LockedPlaceholder) .finish() } } } } impl FrozenVec { /// Returns the number of elements in the vector. pub fn len(&self) -> usize { let vec = self.vec.read().unwrap(); vec.len() } /// Returns `true` if the vector contains no elements. pub fn is_empty(&self) -> bool { self.len() == 0 } } impl FrozenVec { pub fn new() -> Self { 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)) } } /// Returns an iterator over the vector. pub fn iter(&self) -> Iter<'_, T> { self.into_iter() } } /// Iterator over FrozenVec, obtained via `.iter()` /// /// It is safe to push to the vector during iteration #[derive(Debug)] 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()) } impl FrozenVec { /// Returns the internal vector backing this structure /// /// # Examples /// /// ``` /// use elsa::sync::FrozenVec; /// /// let map = FrozenVec::new(); /// map.push("a"); /// map.push("b"); /// let tuple_vec = map.into_vec(); /// /// assert_eq!(tuple_vec, vec!["a", "b"]); /// ``` pub fn into_vec(self) -> Vec { self.vec.into_inner().unwrap() } // TODO add more } impl std::convert::AsMut> for FrozenVec { /// Get mutable access to the underlying vector. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. fn as_mut(&mut self) -> &mut Vec { self.vec.get_mut().unwrap() } } impl Default for FrozenVec { fn default() -> Self { Self { vec: Default::default(), } } } impl Clone for FrozenVec { fn clone(&self) -> Self { Self { vec: self.vec.read().unwrap().clone().into(), } } } impl PartialEq for FrozenVec { fn eq(&self, other: &Self) -> bool { let self_ref: &Vec = &self.vec.read().unwrap(); let other_ref: &Vec = &other.vec.read().unwrap(); self_ref == other_ref } } // The context for these functions is that we want to have a // series of exponentially increasing buffer sizes. We want // to maximize the total size of the buffers (since this // determines the maximum size of the container) whilst // minimizing the number of buffers (since we pay an up-front // cost in space proportional to the number of buffers) // without increasing the buffer size too much each time as // this determines how much space will be wasted on average // in allocated buffers. Finally, we also want a sequence // which will generate nice round numbers and is easy to // work with. /// we multiply the buffer size by 4 each time whilst sizing /// the first buffer to 3, so the buffer sizes generated by /// the function will be 3, 12, 48, 192, etc. const fn buffer_size(idx: usize) -> usize { 3 << (idx * 2) } /// This computes the sum of the sizes of buffers prior to a /// particular buffer index, aka `4^idx - 1`. The choice of /// sequence means that the total buffer size will always be /// a sequence of `1`s in binary, since it's a power of 2 minus one. const fn prior_total_buffer_size(idx: usize) -> usize { (1 << (idx * 2)) - 1 } /// This determines which buffer contains the nth item /// (assuming the items are arranged sequentially in the buffers). /// Since the total buffer sizes are always sequences of 1s in binary, /// we can just count the number of binary digits in `(i+1)` and /// divide by `2` (rounding up). /// (That's what the `(65 - (i + 1).leading_zeros()) >> 1` part does.) /// We use 65 rather than `64` so that the division by `2` rounds /// up instead of down. We divide by `2 (>> 1)` because we skip /// every other power of `2` since we increase the buffer size by `4` /// each time, and finally we subtract one because buffer indices are /// zero-indexed. const fn buffer_index(i: usize) -> usize { (((usize::BITS + 1 - (i + 1).leading_zeros()) >> 1) - 1) as usize } const NUM_BUFFERS: usize = (usize::BITS >> 2) as usize; /// Append-only threadsafe version of `std::vec::Vec` where /// insertion does not require mutable access. /// Does not lock for reading, only allows `Copy` types and /// will spinlock on pushes without affecting reads. /// Note that this data structure is `64` pointers large on /// 64 bit systems, /// in contrast to `Vec` which is `3` pointers large. pub struct LockFreeFrozenVec { data: [AtomicPtr; NUM_BUFFERS], len: AtomicUsize, locked: AtomicBool, } impl Drop for LockFreeFrozenVec { fn drop(&mut self) { // We need to drop the elements from all allocated buffers. for i in 0..NUM_BUFFERS { let layout = Self::layout(buffer_size(i)); unsafe { let ptr = *self.data[i].get_mut(); if ptr.is_null() { // After the first null pointer there will only be more // null pointers. break; } else { std::alloc::dealloc(ptr.cast(), layout); } } } } } impl Default for LockFreeFrozenVec { /// Creates an empty `LockFreeFrozenVec` that does not allocate /// any heap allocations until the first time data is pushed to it. fn default() -> Self { Self { data: [Self::NULL; NUM_BUFFERS], len: AtomicUsize::new(0), locked: AtomicBool::new(false), } } } impl LockFreeFrozenVec { const NULL: AtomicPtr = AtomicPtr::new(std::ptr::null_mut()); pub fn new() -> Self { Default::default() } /// Obtains a write lock that ensures other writing threads /// wait for us to finish. Reading threads are unaffected and /// can concurrently read while we push a new element. fn lock(&self, f: impl FnOnce() -> U) -> U { while self.locked.swap(true, Ordering::Acquire) { // Wheeeee spinlock std::hint::spin_loop(); } let ret = f(); self.locked.store(false, Ordering::Release); ret } fn layout(cap: usize) -> Layout { Layout::array::(cap).unwrap() } // these should never return &T // these should never delete any entries const NOT_ZST: () = if std::mem::size_of::() == 0 { panic!("`LockFreeFrozenVec` cannot be used with ZSTs"); }; /// Pushes an element to the vector, potentially allocating new memory. /// Returns the index at which the element was inserted. pub fn push(&self, val: T) -> usize { // This statement actually does something: it evaluates a constant. #[allow(path_statements)] { Self::NOT_ZST } self.lock(|| { // These values must be consistent with the pointer we got. let len = self.len.load(Ordering::Acquire); let buffer_idx = buffer_index(len); let mut ptr = self.data[buffer_idx].load(Ordering::Acquire); if ptr.is_null() { // Out of memory, allocate more let layout = Self::layout(buffer_size(buffer_idx)); // SAFETY: `LockFreeFrozenVec` statically rejects zsts and the input `ptr` has always been // allocated at the size stated in `cap`. unsafe { ptr = std::alloc::alloc(layout).cast::(); } assert!(!ptr.is_null()); self.data[buffer_idx].store(ptr, Ordering::Release); } let local_index = len - prior_total_buffer_size(buffer_idx); unsafe { ptr.add(local_index).write(val); } // This is written before updating the data pointer. Other `push` calls cannot observe this, // because they are blocked on aquiring the data pointer before they ever read the `len`. // `get` may read the length without synchronization, but that is fine, // as there will be actually the right number of elements stored, or less elements, // in which case you get a spurious `None`. self.len.store(len + 1, Ordering::Release); len }) } /// Load an element (if it exists). This operation is lock-free and /// performs minimal synchronization. pub fn get(&self, index: usize) -> Option { // The length can only grow, so just doing the length check // independently of the read is fine. Worst case we // read an old length value and end up returning `None` even if // another thread already inserted the value. let len = self.len.load(Ordering::Acquire); if index >= len { return None; } let buffer_idx = buffer_index(index); let buffer_ptr = self.data[buffer_idx].load(Ordering::Acquire); let local_index = index - prior_total_buffer_size(buffer_idx); Some(unsafe { *buffer_ptr.add(local_index) }) } pub fn is_empty(&self) -> bool { self.len.load(Ordering::Relaxed) == 0 } /// Load an element (if it exists). This operation is lock-free and /// performs no synchronized atomic operations. This is a useful primitive to /// implement your own data structure with newtypes around the index. pub unsafe fn get_unchecked(&self, index: usize) -> T { let buffer_idx = buffer_index(index); let buffer_ptr = self.data[buffer_idx].load(Ordering::Relaxed); let local_index = index - prior_total_buffer_size(buffer_idx); unsafe { *buffer_ptr.add(local_index) } } /// Run a function on each buffer in the vector. /// /// ## Arguments /// - `func`: a function that takes a slice to the buffer and the buffer index /// fn for_each_buffer(&self, mut func: impl FnMut(&[T], usize)) { // for each buffer, run the function for buffer_index in 0..NUM_BUFFERS { // get the buffer pointer if let Some(buffer_ptr) = NonNull::new(self.data[buffer_index].load(Ordering::Acquire)) { // get the buffer size and index let buffer_size = buffer_size(buffer_index); // create a slice from the buffer pointer and size let buffer_slice = unsafe { std::slice::from_raw_parts(buffer_ptr.as_ptr(), buffer_size) }; // run the function func(buffer_slice, buffer_index); } else { // no data in this buffer, so we're done break; } } } } impl PartialEq for LockFreeFrozenVec { fn eq(&self, other: &Self) -> bool { // first check the length let self_len = self.len.load(Ordering::Acquire); let other_len = other.len.load(Ordering::Acquire); if self_len != other_len { return false; } // Since the lengths are the same, just check the elements in order for index in 0..self_len { // This is safe because the indices are in bounds (for `LockFreeFrozenVec` the bounds can only grow). if self.get(index) != other.get(index) { return false; } } return true; } } #[test] fn test_non_lockfree_unchecked() { #[derive(Copy, Clone, Debug, PartialEq, Eq)] struct Moo(i32); let vec = LockFreeFrozenVec::new(); let idx_set = std::sync::Mutex::new(std::collections::HashSet::new()); std::thread::scope(|s| { s.spawn(|| { for i in 0..1000 { idx_set.lock().unwrap().insert(vec.push(Moo(i))); } }); s.spawn(|| { for i in 0..1000 { idx_set.lock().unwrap().insert(vec.push(Moo(i))); } }); for _ in 0..2000 { let idxes = std::mem::take(&mut *idx_set.lock().unwrap()); for idx in idxes { unsafe { vec.get_unchecked(idx); } } } }); // Test dropping empty vecs LockFreeFrozenVec::<()>::new(); } impl Clone for LockFreeFrozenVec { fn clone(&self) -> Self { let mut coppied_data = [Self::NULL; NUM_BUFFERS]; // for each buffer, copy the data self.for_each_buffer(|buffer_slice, buffer_index| { // allocate a new buffer let layout = Self::layout(buffer_slice.len()); let new_buffer_ptr = unsafe { std::alloc::alloc(layout).cast::() }; assert!(!new_buffer_ptr.is_null()); // copy the data to the new buffer unsafe { std::ptr::copy_nonoverlapping( buffer_slice.as_ptr(), new_buffer_ptr, buffer_slice.len(), ); }; // store the new buffer pointer *coppied_data[buffer_index].get_mut() = new_buffer_ptr; }); return Self { data: coppied_data, len: AtomicUsize::new(self.len.load(Ordering::Relaxed)), locked: AtomicBool::new(false), }; } } #[test] fn test_non_lockfree() { #[derive(Copy, Clone, Debug, PartialEq, Eq)] struct Moo(i32); let vec = LockFreeFrozenVec::new(); assert_eq!(vec.get(1), None); vec.push(Moo(1)); let i = vec.push(Moo(2)); vec.push(Moo(3)); assert_eq!(vec.get(i), Some(Moo(2))); std::thread::scope(|s| { s.spawn(|| { for i in 0..1000 { vec.push(Moo(i)); } }); s.spawn(|| { for i in 0..1000 { vec.push(Moo(i)); } }); for i in 0..2000 { while vec.get(i).is_none() {} } }); // Test cloning { let vec2 = vec.clone(); assert_eq!(vec2.get(0), Some(Moo(1))); assert_eq!(vec2.get(1), Some(Moo(2))); assert_eq!(vec2.get(2), Some(Moo(3))); } // Test cloning a large vector { let large_vec = LockFreeFrozenVec::new(); for i in 0..1000 { large_vec.push(Moo(i)); } let large_vec_2 = large_vec.clone(); for i in 0..1000 { assert_eq!(large_vec_2.get(i), Some(Moo(i as i32))); } } // Test cloning an empty vector { let empty_vec = LockFreeFrozenVec::<()>::new(); let empty_vec_2 = empty_vec.clone(); assert_eq!(empty_vec_2.get(0), None); } // Test dropping empty vecs LockFreeFrozenVec::<()>::new(); } // TODO: Implement IntoIterator for LockFreeFrozenVec /// 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 FrozenBTreeMap { /// Collects the contents of this map into a vector of tuples. /// /// The order of the entries is as if iterating a [`BTreeMap`] (ordered by key). /// /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// map.insert(2, Box::new("b")); /// let tuple_vec = map.into_tuple_vec(); /// /// assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]); /// ``` pub fn into_tuple_vec(self) -> Vec<(K, V)> { self.0.into_inner().unwrap().into_iter().collect::>() } /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// assert_eq!(map.len(), 0); /// map.insert(1, Box::new("a")); /// assert_eq!(map.len(), 1); /// ``` pub fn len(&self) -> usize { let map = self.0.read().unwrap(); map.len() } /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// assert_eq!(map.is_empty(), true); /// map.insert(1, Box::new("a")); /// assert_eq!(map.is_empty(), false); /// ``` pub fn is_empty(&self) -> bool { let map = self.0.read().unwrap(); map.is_empty() } } impl From> for FrozenBTreeMap { fn from(map: BTreeMap) -> Self { Self(RwLock::new(map)) } } impl Index<&Q> for FrozenBTreeMap where Q: Ord, K: Clone + Ord + Borrow, V: StableDeref, { type Output = V::Target; /// # Examples /// /// ``` /// use elsa::sync::FrozenBTreeMap; /// /// let map = FrozenBTreeMap::new(); /// map.insert(1, Box::new("a")); /// assert_eq!(map[&1], "a"); /// ``` fn index(&self, idx: &Q) -> &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() } } impl Clone for FrozenBTreeMap { fn clone(&self) -> Self { Self(self.0.read().unwrap().clone().into()) } } impl PartialEq for FrozenBTreeMap { fn eq(&self, other: &Self) -> bool { let self_ref: &BTreeMap = &self.0.read().unwrap(); let other_ref: &BTreeMap = &other.0.read().unwrap(); self_ref == other_ref } } elsa-1.10.0/src/vec.rs000064400000000000000000000233531046102023000125470ustar 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) } } } impl FrozenVec { /// 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) } } impl FrozenVec { /// Returns a copy of an element. pub fn get_copy(&self, index: usize) -> Option { unsafe { let vec = self.vec.get(); (*vec).get(index).copied() } } } impl FrozenVec { /// 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 } } impl FrozenVec { /// 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() } } impl FrozenVec { /// Converts the frozen vector into a plain vector. pub fn into_vec(self) -> Vec { self.vec.into_inner() } } impl FrozenVec { // 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 std::convert::AsMut> for FrozenVec { /// Get mutable access to the underlying vector. /// /// This is safe, as it requires a `&mut self`, ensuring nothing is using /// the 'frozen' contents. fn as_mut(&mut self) -> &mut Vec { unsafe { &mut *self.vec.get() } } } impl Default for FrozenVec { fn default() -> Self { FrozenVec::new() } } impl Clone for FrozenVec { fn clone(&self) -> Self { Self { vec: unsafe { self.vec.get().as_ref().unwrap() }.clone().into(), } } } 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 } } } impl PartialEq for FrozenVec where T::Target: PartialEq, { fn eq(&self, other: &Self) -> bool { unsafe { self.vec.get().as_ref() == other.vec.get().as_ref() } } } #[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_non_stable_deref() { #[derive(Copy, Clone, Debug, PartialEq, Eq)] struct Moo(i32); let vec: FrozenVec = FrozenVec::new(); assert_eq!(vec.is_empty(), true); assert_eq!(vec.len(), 0); assert_eq!(vec.get_copy(1), None); vec.push(Moo(1)); vec.push(Moo(2)); vec.push(Moo(3)); assert_eq!(vec.is_empty(), false); assert_eq!(vec.len(), 3); assert_eq!(vec.get_copy(1), Some(Moo(2))); } #[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); }