Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
pub(crate) use self::inner::AllocError;
pub(crate) use self::inner::{Allocator, Global, do_alloc};

// Since `ToOwned` is defined in `alloc`, this would prevent us from using
// this crate in `alloc`; that would be a circular dependency. To get around
// this particular restriction for `ToOwned`, we just make our own `ToOwned`
// trait here that `alloc` can blanket-forward to its own definition of
// `ToOwned` instead.
#[cfg(feature = "rustc-dep-of-std")]
#[expect(missing_docs)]
pub trait ToOwned {
type Owned: core::borrow::Borrow<Self>;
fn to_owned(&self) -> Self::Owned;
}

// In every other case, we just use the regular `ToOwned`.
#[cfg(not(feature = "rustc-dep-of-std"))]
pub(crate) use stdalloc::borrow::ToOwned;

// Nightly-case.
// Use unstable `allocator_api` feature.
// This is compatible with `allocator-api2` which can be enabled or not.
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub use crate::hasher::DefaultHashBuilder;
#[cfg(feature = "default-hasher")]
pub use crate::hasher::DefaultHasher;

#[cfg(feature = "rustc-dep-of-std")]
pub use crate::alloc::ToOwned;

pub mod hash_map {
//! A hash map implemented with quadratic probing and SIMD lookup.
pub use crate::map::*;
Expand Down
5 changes: 2 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::alloc::ToOwned;
use crate::alloc::{Allocator, Global};
use crate::raw::{Bucket, RawDrain, RawExtractIf, RawIntoIter, RawIter, RawTable};
use crate::{DefaultHashBuilder, Equivalent, TryReserveError};
Expand All @@ -8,7 +9,6 @@ use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::mem;
use core::ops::Index;
use stdalloc::borrow::ToOwned;

#[cfg(feature = "raw-entry")]
pub use crate::raw_entry::*;
Expand Down Expand Up @@ -5073,12 +5073,11 @@ mod test_map {
use super::Entry::{Occupied, Vacant};
use super::EntryRef;
use super::HashMap;
use crate::alloc::{AllocError, Allocator, Global};
use crate::alloc::{AllocError, Allocator, Global, ToOwned};
use core::alloc::Layout;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicI8, Ordering};
use rand::{Rng, SeedableRng, rngs::SmallRng};
use std::borrow::ToOwned;
use std::cell::RefCell;
use std::vec::Vec;
use stdalloc::string::String;
Expand Down
8 changes: 7 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,13 @@ where
entry.remove();
}
map::EntryRef::Vacant(entry) => {
entry.insert(());
// SAFETY: We know that `item.clone()` is equivalent to `item`.
//
// This is *required* to get around the lack of *any* impl
// for `ToOwned` when compiling as `rustc-dep-of-std`.
unsafe {
entry.insert_with_key_unchecked(item.clone(), ());
}
}
}
}
Expand Down
Loading