Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with 1.34 #3

Merged
merged 10 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
command: check
toolchain: "1.34.0"
override: true

jyn514 marked this conversation as resolved.
Show resolved Hide resolved
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
2 changes: 1 addition & 1 deletion src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl<T: Sized + Clone> Bucket<T> {
self.index += slice.len();

// Safety: The caller promises to forget the reference before the arena is dropped
&*ptr::slice_from_raw_parts(ptr, slice.len())
target
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(feature = "no-std", no_std)]
#![cfg_attr(feature = "nightly", feature(hash_raw_entry))]
// `.copied()` was unstable in 1.34
#![allow(clippy::map_clone)]
#![warn(clippy::missing_inline_in_public_items)]
#![deny(
missing_docs,
Expand Down
29 changes: 9 additions & 20 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
util::{Iter, Strings},
};

use core::{hash::BuildHasher, mem};
use core::hash::BuildHasher;

compile! {
if #[feature = "no-std"] {
Expand Down Expand Up @@ -82,7 +82,7 @@ where
where
T: AsRef<V>,
{
self.map.get(val.as_ref()).copied()
self.map.get(val.as_ref()).map(|&k| k)
}

/// Resolves a string by its key. Only keys made by the current Resolver or the creator
Expand Down Expand Up @@ -251,27 +251,16 @@ where
/// [`RodeoResolver`]: crate::RodeoResolver
#[inline]
#[must_use]
pub fn into_resolver(mut self) -> RodeoResolver<V, K> {
self.map.drain().for_each(drop);
pub fn into_resolver(self) -> RodeoResolver<V, K> {
let RodeoReader {
map: _map,
strings,
arena,
} = self;

// Safety: The current reader no longer contains references to the strings
// in the vec given to RodeoResolver
unsafe { RodeoResolver::new(mem::take(&mut self.strings), mem::take(&mut self.arena)) }
}
}

/// Deallocate the leaked strings interned by RodeoReader
impl<V, K, S> Drop for RodeoReader<V, K, S>
where
V: Internable + ?Sized,
K: Key,
S: BuildHasher + Clone,
{
#[inline]
fn drop(&mut self) {
// Safety: There must not be any other references to the strings in the arena, so
// all strings are drained before the arena can drop
self.strings.drain(..).for_each(drop);
unsafe { RodeoResolver::new(strings, arena) }
}
}

Expand Down
49 changes: 16 additions & 33 deletions src/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
util::{Iter, Strings},
};

use core::{hash::BuildHasher, mem};
use core::hash::BuildHasher;

compile! {
if #[feature = "no-std"] {
Expand Down Expand Up @@ -297,7 +297,7 @@ where
where
T: AsRef<V>,
{
self.map.get(val.as_ref()).copied()
self.map.get(val.as_ref()).map(|&k| k)
}

/// Resolves a string by its key. Only keys made by the current Rodeo may be used
Expand Down Expand Up @@ -474,18 +474,15 @@ where
/// [`RodeoReader`]: crate::RodeoReader
#[inline]
#[must_use]
pub fn into_reader(mut self) -> RodeoReader<V, K, S> {
let mut map = HashMap::with_capacity_and_hasher(self.map.len(), self.map.hasher().clone());
map.extend(self.map.drain());
pub fn into_reader(self) -> RodeoReader<V, K, S> {
let Self {
map,
strings,
arena,
} = self;

// Safety: No other references outside of `map` and `strings` to the interned strings exist
unsafe {
RodeoReader::new(
map,
mem::take(&mut self.strings),
mem::take(&mut self.arena),
)
}
unsafe { RodeoReader::new(map, strings, arena) }
}

/// Consumes the current Rodeo, returning a [`RodeoResolver`] to allow contention-free access of the interner
Expand All @@ -509,11 +506,15 @@ where
/// [`RodeoResolver`]: crate::RodeoResolver
#[inline]
#[must_use]
pub fn into_resolver(mut self) -> RodeoResolver<V, K> {
self.map.drain().for_each(drop);
pub fn into_resolver(self) -> RodeoResolver<V, K> {
let Rodeo {
map: _map,
strings,
arena,
} = self;

// Safety: No other references to the strings exist
unsafe { RodeoResolver::new(mem::take(&mut self.strings), mem::take(&mut self.arena)) }
unsafe { RodeoResolver::new(strings, arena) }
}
}

Expand All @@ -528,24 +529,6 @@ impl Default for Rodeo<str, Spur, RandomState> {
}
}

/// Deallocate the leaked strings interned by Rodeo
impl<V, K, S> Drop for Rodeo<V, K, S>
where
V: Internable + ?Sized,
K: Key,
S: BuildHasher + Clone,
{
#[inline]
fn drop(&mut self) {
// Clear the map to remove all other references to the strings in self.strings
self.map.clear();

// Safety: There must not be any other references to the strings in the arena, so
// all strings are drained before the arena can drop
self.strings.drain(..).for_each(drop);
}
}

unsafe impl<V, K, S> Send for Rodeo<V, K, S>
where
V: Internable + ?Sized + Send,
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().copied()
self.iter.next().map(|&k| k)
}

#[inline]
Expand Down