diff --git a/Cargo.toml b/Cargo.toml index 0376e5a..9931ccc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,11 @@ name = "veccell" version = "0.1.0" edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +authors = [ "Shad Amethyst " ] +description = "Provides VecCell, a variant of Vec with interior mutability." +repository = "https://github.com/adri326/veccell/" +license = "MIT OR Apache-2.0" +keywords = ["vec", "refcell", "interior-mutability"] +categories = ["rust-patterns"] [dependencies] diff --git a/LICENSE-apache.txt b/LICENSE-apache.txt new file mode 100644 index 0000000..cf5708d --- /dev/null +++ b/LICENSE-apache.txt @@ -0,0 +1,13 @@ +Copyright 2022 Adrien Burgun + +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. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..049af33 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright © 2022 Adrien Burgun + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..00df822 --- /dev/null +++ b/README.md @@ -0,0 +1,113 @@ +# VecCell + +A variant of `Vec`, with interior mutability, where one element may be borrowed mutably and several other elements may be borrowed immutably at the same time. + +You would use this crate if: + +- You need a `Vec` with interior mutability +- You only want mutable access to one element at a time +- You want immutable access to all other elements while an element is borrowed mutably +- You need a constant memory cost + +You would need something else if: + +- You don't need interior mutability *(you may use `Vec` instead)* +- While an element is borrowed mutably, you don't need to access the others *(you may use `RefCell>` instead)* +- You want mutable access to multiple elements at a time *(you may use `Vec>` instead)* +- You need to share the array across multiple threads *(you may use `Vec>` or `Arc>>` instead)* + +## Installation + +Run `cargo add veccell` or add the following in `Cargo.toml`: + +```toml +[dependencies] +veccell = "0.1.0" +``` + +## Examples + +`VecCell` allows an element to be borrowed mutably, and other elements to be accessed immutably: + +```rust +use veccell::VecCell; + +let mut arr: VecCell = VecCell::new(); + +arr.push(32); +arr.push(48); +arr.push(2); + +let mut third = arr.borrow_mut(2).unwrap(); // Borrow the third element mutably +let first = arr.get(0).unwrap(); // Borrow the first element immutably + +*third *= *first; // Multiply the third element by the first element + +println!("{}", third.get()); // Prints 64 +std::mem::drop(third); // Drop the mutable borrow + +println!("{}", arr.get(2).unwrap().get()); // Also prints 64 +``` + +However, to prevent aliasing, while an element is borrowed mutably, it cannot be borrowed immutably: + +```rust +use veccell::VecCell; + +let mut arr: VecCell = VecCell::new(); + +arr.push(32); +arr.push(48); +arr.push(8); + +let mut third = arr.borrow_mut(2).unwrap(); // Borrow the third element mutably + +// Here, arr.get(2) returns None, +// because the third element is already borrowed mutably. +let third2 = arr.get(2); + +assert!(third2.is_none()); + +std::mem::drop(third); +``` + +`VecCell` only stores two additional pieces of information: + +- which element is currently borrowed mutably (accessible through `mut_borrow`) +- how many elements are borrowed immutably (accessible through `borrows`) + +This has the advantage of putting all of its elements next to each other in memory, if their padding allows it, but has the disadvantage of having more restrictive rules than you'd expect: + +To borrow an element mutably, no element must be borrowed mutably *or immutably*: + +```rust +use veccell::VecCell; + +let mut arr: VecCell = VecCell::new(); + +arr.push(32); +arr.push(48); +arr.push(8); + +let second = arr.get(1).unwrap(); + +let third = arr.borrow_mut(2); + +// VecCell has no way of telling that the existing immutable borrow (`second`) +// isn't borrowing the third element. +assert!(third.is_none()); + +std::mem::drop(third); + +let first = arr.borrow_mut(0); + +// VecCell can only allow one mutable borrow at a time +assert!(arr.borrow_mut(1).is_none()); +``` + +## License + +This project is dual-licensed under the MIT license and the Apache v2.0 license. +You may choose either of those when using this library. + +Any contribution to this repository must be made available under both licenses. diff --git a/src/lib.rs b/src/lib.rs index 5f7bd51..600200c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! You would use this crate if: //! - You need a `Vec` with [interior mutability](std::cell#when-to-choose-interior-mutability) //! - You only want mutable access to one element at a time -//! - You want immutable access to all other elements while an element is borrowed +//! - You want immutable access to all other elements while an element is borrowed mutably //! - You need a constant memory cost //! //! You would need something else if: @@ -1155,3 +1155,7 @@ mod test { std::mem::drop(y); } } + +#[doc = include_str!("../README.md")] +#[cfg(doctest)] +pub struct ReadmeDoctests;