Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
name = "toolshed"
version = "0.7.0"
version = "0.8.0"
authors = ["maciejhirsz <maciej.hirsz@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Arena allocator and a handful of useful data structures"
repository = "https://github.com/ratel-rust/toolshed"
documentation = "https://docs.rs/toolshed/"
readme = "README.md"
edition = "2018"

[dependencies]
fxhash = "0.2"
rustc-hash = "1.0"
serde = { version = "1.0", optional = true }

[dev-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions benches/bloomset.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![feature(test)]
extern crate test;
extern crate fxhash;
extern crate toolshed;

use toolshed::set::{BloomSet, Set};
use toolshed::Arena;
use test::{Bencher, black_box};
use std::collections::HashSet;
use fxhash::FxHashSet;
use rustc_hash::FxHashSet;

static WORDS: &[&str] = &[
"ARENA_BLOCK", "Arena", "Cell", "Self", "String", "T", "Vec", "_unchecked", "a",
Expand Down
2 changes: 0 additions & 2 deletions benches/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(test)]
extern crate test;
extern crate fxhash;
extern crate toolshed;

use toolshed::list::ListBuilder;
use toolshed::Arena;
Expand Down
13 changes: 5 additions & 8 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Arena {
}

/// A pointer to an uninitialized region of memory.
pub struct Uninitialized<'arena, T: Copy + 'arena> {
pub struct Uninitialized<'arena, T: Copy> {
pointer: &'arena mut MaybeUninit<T>,
}

Expand All @@ -33,7 +33,7 @@ union MaybeUninit<T: Copy> {
_uninit: (),
}

impl<'arena, T: Copy + 'arena> Uninitialized<'arena, T> {
impl<'arena, T: Copy> Uninitialized<'arena, T> {
/// Initialize the memory at the pointer with a given value.
#[inline]
pub fn init(self, value: T) -> &'arena mut T {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<'arena, T: Copy + 'arena> Uninitialized<'arena, T> {
}
}

impl<'arena, T: Copy + 'arena> From<&'arena mut T> for Uninitialized<'arena, T> {
impl<'arena, T: Copy> From<&'arena mut T> for Uninitialized<'arena, T> {
#[inline]
fn from(pointer: &'arena mut T) -> Self {
unsafe { Self::from_raw(pointer) }
Expand Down Expand Up @@ -102,7 +102,6 @@ impl<'arena> NulTermStr<'arena> {
/// would otherwise have to be length checks.
///
/// ```rust
/// # extern crate toolshed;
/// # use toolshed::Arena;
/// # fn main() {
/// let arena = Arena::new();
Expand Down Expand Up @@ -452,11 +451,9 @@ mod test {
assert_eq!(arena.offset.get(), 16);
assert_eq!(
allocated,
&[
b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', 0
]
"abcdefghijk\u{0}".as_bytes(),
);

assert_eq!(nts.to_string(), "abcdefghijk");
assert_eq!(&**nts, "abcdefghijk");
}
}
37 changes: 7 additions & 30 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::marker::PhantomData;
/// This should be identical to the `Cell` implementation in the standard
/// library, but always require that the internal type implements `Copy`
/// and implements `Copy` itself.
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Copy, Clone)]
#[repr(transparent)]
pub struct CopyCell<T> {
/// Internal value
Expand All @@ -23,8 +23,7 @@ unsafe impl<T> Send for CopyCell<T> {}

impl<T> CopyCell<T> {
/// Creates a new `CopyCell` containing the given value.
#[inline]
pub fn new(value: T) -> Self {
pub const fn new(value: T) -> Self {
CopyCell {
value,
_no_sync: PhantomData
Expand All @@ -39,19 +38,6 @@ impl<T: Copy> CopyCell<T> {
self.value
}

/// Returns a mutable reference to the underlying data.
///
/// This call borrows `CopyCell` mutably, which gives us a compile time
/// memory safety guarantee.
#[inline]
pub fn get_mut<'a>(&'a mut self) -> &'a mut T {
// We can just cast the pointer from `CopyCell<T>` to `T` because of
// #[repr(transparent)]
unsafe {
&mut *(self as *mut CopyCell<T> as *mut T)
}
}

/// Sets the contained value.
#[inline]
pub fn set(&self, value: T) {
Expand All @@ -71,19 +57,10 @@ impl<T: Copy> CopyCell<T> {
}
}

impl<T: Copy> Clone for CopyCell<T> {
#[inline]
fn clone(&self) -> CopyCell<T> {
CopyCell::new(self.get())
}
}

impl<T: Copy> Copy for CopyCell<T> { }

impl<T: Debug + Copy> Debug for CopyCell<T> {
impl<T: Debug> Debug for CopyCell<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.get(), f)
Debug::fmt(&self.value, f)
}
}

Expand All @@ -94,8 +71,8 @@ mod test {
#[test]
fn cell() {
let cell_a = CopyCell::new(42u64);
let mut cell_b = cell_a; // copy
let cell_c = &cell_a; // reference
let cell_b = cell_a; // copy
let cell_c = &cell_a; // reference

assert_eq!(cell_a.get(), 42);
assert_eq!(cell_b.get(), 42);
Expand All @@ -116,7 +93,7 @@ mod test {
assert_eq!(cell_c.get(), 200);

// Again, only affects the copy
*cell_b.get_mut() = 300;
cell_b.set(300);

assert_eq!(cell_a.get(), 200);
assert_eq!(cell_b.get(), 300);
Expand Down
8 changes: 4 additions & 4 deletions src/impl_debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Debug};
use list::{List, GrowableList, ListBuilder};
use map::{Map, BloomMap};
use set::{Set, BloomSet};
use crate::list::{List, GrowableList, ListBuilder};
use crate::map::{Map, BloomMap};
use crate::set::{Set, BloomSet};

impl<'arena, T> Debug for List<'arena, T>
where
Expand Down Expand Up @@ -78,7 +78,7 @@ where
#[cfg(test)]
mod test {
use super::*;
use Arena;
use crate::Arena;

#[test]
fn list_debug() {
Expand Down
6 changes: 3 additions & 3 deletions src/impl_partial_eq.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use list::List;
use map::{Map, BloomMap};
use set::{Set, BloomSet};
use crate::list::List;
use crate::map::{Map, BloomMap};
use crate::set::{Set, BloomSet};

impl<'a, 'b, A, B> PartialEq<List<'b, B>> for List<'a, A>
where
Expand Down
8 changes: 4 additions & 4 deletions src/impl_serialize.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::ser::{Serialize, Serializer};
use list::List;
use map::{Map, BloomMap};
use set::{Set, BloomSet};
use crate::list::List;
use crate::map::{Map, BloomMap};
use crate::set::{Set, BloomSet};

impl<'arena, T> Serialize for List<'arena, T>
where
Expand Down Expand Up @@ -74,7 +74,7 @@ where
mod test {
use super::*;
use serde_json;
use Arena;
use crate::Arena;

#[test]
fn list_can_be_serialized() {
Expand Down
13 changes: 4 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
//! ## Example
//!
//! ```rust
//!
//! extern crate toolshed;
//!
//! use toolshed::Arena;
//! use toolshed::map::Map;
//!
Expand Down Expand Up @@ -92,13 +89,11 @@

// Pull in serde if `impl_serialize` is enabled
#[cfg(feature = "impl_serialize")]
extern crate serde;
use serde;

// Pull in serde_json for testing if `impl_serialize` is enabled
#[cfg(all(test, feature = "impl_serialize"))]
extern crate serde_json;

extern crate fxhash;
use serde_json;

mod cell;
pub mod map;
Expand All @@ -112,5 +107,5 @@ mod impl_debug;
#[cfg(feature = "impl_serialize")]
mod impl_serialize;

pub use arena::{Arena, Uninitialized, NulTermStr};
pub use cell::CopyCell;
pub use self::arena::{Arena, Uninitialized, NulTermStr};
pub use self::cell::CopyCell;
Loading