Skip to content

Commit 230ea7e

Browse files
upstream stdx changes
1 parent 0d64633 commit 230ea7e

File tree

11 files changed

+185
-101
lines changed

11 files changed

+185
-101
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stdx/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ rust-version.workspace = true
1313

1414
[dependencies]
1515
backtrace = { version = "0.3.74", optional = true }
16-
jod-thread = "0.1.2"
16+
jod-thread = "1.0.0"
1717
crossbeam-channel.workspace = true
1818
itertools.workspace = true
1919
tracing.workspace = true

crates/stdx/src/anymap.rs

+54-50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! This file is a port of only the necessary features from <https://github.com/chris-morgan/anymap> version 1.0.0-beta.2 for use within rust-analyzer.
2+
//!
23
//! Copyright © 2014–2022 Chris Morgan.
34
//! COPYING: <https://github.com/chris-morgan/anymap/blob/master/COPYING>
45
//! Note that the license is changed from Blue Oak Model 1.0.0 or MIT or Apache-2.0 to MIT OR Apache-2.0
@@ -20,14 +21,14 @@
2021

2122
use core::hash::Hasher;
2223

23-
/// A hasher designed to eke a little more speed out, given `TypeId`s known characteristics.
24+
/// A hasher designed to eke a little more speed out, given `TypeId`'s known characteristics.
2425
///
25-
/// Specifically, this is a no-op hasher that expects to be fed a u64s worth of
26+
/// Specifically, this is a no-op hasher that expects to be fed a u64's worth of
2627
/// randomly-distributed bits. It works well for `TypeId` (eliminating start-up time, so that my
27-
/// get_missing benchmark is ~30ns rather than ~900ns, and being a good deal faster after that, so
28-
/// that my insert_and_get_on_260_types benchmark is ~12μs instead of ~21.5μs), but will
28+
/// `get_missing` benchmark is ~30ns rather than ~900ns, and being a good deal faster after that, so
29+
/// that my `insert_and_get_on_260_types` benchmark is ~12μs instead of ~21.5μs), but will
2930
/// panic in debug mode and always emit zeros in release mode for any other sorts of inputs, so
30-
/// yeah, dont use it! 😀
31+
/// yeah, don't use it! 😀
3132
#[derive(Default)]
3233
pub struct TypeIdHasher {
3334
value: u64,
@@ -36,9 +37,9 @@ pub struct TypeIdHasher {
3637
impl Hasher for TypeIdHasher {
3738
#[inline]
3839
fn write(&mut self, bytes: &[u8]) {
39-
// This expects to receive exactly one 64-bit value, and theres no realistic chance of
40-
// that changing, but I dont want to depend on something that isnt expressly part of the
41-
// contract for safety. But Im OK with release builds putting everything in one bucket
40+
// This expects to receive exactly one 64-bit value, and there's no realistic chance of
41+
// that changing, but I don't want to depend on something that isn't expressly part of the
42+
// contract for safety. But I'm OK with release builds putting everything in one bucket
4243
// if it *did* change (and debug builds panicking).
4344
debug_assert_eq!(bytes.len(), 8);
4445
let _ = bytes.try_into().map(|array| self.value = u64::from_ne_bytes(array));
@@ -59,7 +60,7 @@ use ::std::collections::hash_map;
5960
/// Raw access to the underlying `HashMap`.
6061
///
6162
/// This alias is provided for convenience because of the ugly third generic parameter.
62-
#[allow(clippy::disallowed_types)] // Uses a custom hasher
63+
#[expect(clippy::disallowed_types, reason = "Uses a custom hasher")]
6364
pub type RawMap<A> = hash_map::HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>;
6465

6566
/// A collection containing zero or one values for any given type and allowing convenient,
@@ -73,19 +74,20 @@ pub type RawMap<A> = hash_map::HashMap<TypeId, Box<A>, BuildHasherDefault<TypeId
7374
///
7475
/// Cumulatively, there are thus six forms of map:
7576
///
76-
/// - <code>[Map]&lt;dyn [core::any::Any]&gt;</code>,
77+
/// - `[Map]<dyn [core::any::Any]>`,
7778
/// also spelled [`AnyMap`] for convenience.
78-
/// - <code>[Map]&lt;dyn [core::any::Any] + Send&gt;</code>
79-
/// - <code>[Map]&lt;dyn [core::any::Any] + Send + Sync&gt;</code>
79+
/// - `[Map]<dyn [core::any::Any] + Send>`
80+
/// - `[Map]<dyn [core::any::Any] + Send + Sync>`
8081
///
8182
/// ## Example
8283
///
83-
/// (Here using the [`AnyMap`] convenience alias; the first line could use
84-
/// <code>[anymap::Map][Map]::&lt;[core::any::Any]&gt;::new()</code> instead if desired.)
84+
/// (Here, the [`AnyMap`] convenience alias is used;
85+
/// the first line could use `[anymap::Map][Map]::<[core::any::Any]>::new()`
86+
/// instead if desired.)
8587
///
8688
/// ```
8789
/// # use stdx::anymap;
88-
#[doc = "let mut data = anymap::AnyMap::new();"]
90+
/// let mut data = anymap::AnyMap::new();
8991
/// assert_eq!(data.get(), None::<&i32>);
9092
/// ```
9193
///
@@ -95,29 +97,31 @@ pub struct Map<A: ?Sized + Downcast = dyn Any> {
9597
raw: RawMap<A>,
9698
}
9799

98-
/// The most common type of `Map`: just using `Any`; <code>[Map]&lt;dyn [Any]&gt;</code>.
100+
/// The most common type of `Map`: just using `Any`; `[Map]<dyn [Any]>`.
99101
///
100102
/// Why is this a separate type alias rather than a default value for `Map<A>`?
101-
/// `Map::new()` doesnt seem to be happy to infer that it should go with the default
102-
/// value. Its a bit sad, really. Ah well, I guess this approach will do.
103+
/// `Map::new()` doesn't seem to be happy to infer that it should go with the default
104+
/// value. It's a bit sad, really. Ah well, I guess this approach will do.
103105
pub type AnyMap = Map<dyn Any>;
104106
impl<A: ?Sized + Downcast> Default for Map<A> {
105107
#[inline]
106-
fn default() -> Map<A> {
107-
Map::new()
108+
fn default() -> Self {
109+
Self::new()
108110
}
109111
}
110112

111113
impl<A: ?Sized + Downcast> Map<A> {
112114
/// Create an empty collection.
113115
#[inline]
114-
pub fn new() -> Map<A> {
115-
Map { raw: RawMap::with_hasher(Default::default()) }
116+
#[must_use]
117+
pub fn new() -> Self {
118+
Self { raw: RawMap::with_hasher(BuildHasherDefault::default()) }
116119
}
117120

118121
/// Returns a reference to the value stored in the collection for the type `T`,
119122
/// if it exists.
120123
#[inline]
124+
#[must_use]
121125
pub fn get<T: IntoBox<A>>(&self) -> Option<&T> {
122126
self.raw.get(&TypeId::of::<T>()).map(|any| unsafe { any.downcast_ref_unchecked::<T>() })
123127
}
@@ -137,51 +141,52 @@ impl<A: ?Sized + Downcast> Map<A> {
137141
}
138142

139143
/// A view into a single occupied location in an `Map`.
140-
pub struct OccupiedEntry<'a, A: ?Sized + Downcast, V: 'a> {
141-
inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>>,
144+
pub struct OccupiedEntry<'map, A: ?Sized + Downcast, V: 'map> {
145+
inner: hash_map::OccupiedEntry<'map, TypeId, Box<A>>,
142146
type_: PhantomData<V>,
143147
}
144148

145149
/// A view into a single empty location in an `Map`.
146-
pub struct VacantEntry<'a, A: ?Sized + Downcast, V: 'a> {
147-
inner: hash_map::VacantEntry<'a, TypeId, Box<A>>,
150+
pub struct VacantEntry<'map, A: ?Sized + Downcast, V: 'map> {
151+
inner: hash_map::VacantEntry<'map, TypeId, Box<A>>,
148152
type_: PhantomData<V>,
149153
}
150154

151155
/// A view into a single location in an `Map`, which may be vacant or occupied.
152-
pub enum Entry<'a, A: ?Sized + Downcast, V> {
156+
pub enum Entry<'map, A: ?Sized + Downcast, V> {
153157
/// An occupied Entry
154-
Occupied(OccupiedEntry<'a, A, V>),
158+
Occupied(OccupiedEntry<'map, A, V>),
155159
/// A vacant Entry
156-
Vacant(VacantEntry<'a, A, V>),
160+
Vacant(VacantEntry<'map, A, V>),
157161
}
158162

159-
impl<'a, A: ?Sized + Downcast, V: IntoBox<A>> Entry<'a, A, V> {
163+
impl<'map, A: ?Sized + Downcast, V: IntoBox<A>> Entry<'map, A, V> {
160164
/// Ensures a value is in the entry by inserting the result of the default function if
161165
/// empty, and returns a mutable reference to the value in the entry.
162166
#[inline]
163-
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
167+
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'map mut V {
164168
match self {
165169
Entry::Occupied(inner) => inner.into_mut(),
166170
Entry::Vacant(inner) => inner.insert(default()),
167171
}
168172
}
169173
}
170174

171-
impl<'a, A: ?Sized + Downcast, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
172-
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
175+
impl<'map, A: ?Sized + Downcast, V: IntoBox<A>> OccupiedEntry<'map, A, V> {
176+
/// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
173177
/// with a lifetime bound to the collection itself
174178
#[inline]
175-
pub fn into_mut(self) -> &'a mut V {
179+
#[must_use]
180+
pub fn into_mut(self) -> &'map mut V {
176181
unsafe { self.inner.into_mut().downcast_mut_unchecked() }
177182
}
178183
}
179184

180-
impl<'a, A: ?Sized + Downcast, V: IntoBox<A>> VacantEntry<'a, A, V> {
181-
/// Sets the value of the entry with the VacantEntry's key,
185+
impl<'map, A: ?Sized + Downcast, V: IntoBox<A>> VacantEntry<'map, A, V> {
186+
/// Sets the value of the entry with the `VacantEntry`'s key,
182187
/// and returns a mutable reference to it
183188
#[inline]
184-
pub fn insert(self, value: V) -> &'a mut V {
189+
pub fn insert(self, value: V) -> &'map mut V {
185190
unsafe { self.inner.insert(value.into_box()).downcast_mut_unchecked() }
186191
}
187192
}
@@ -206,14 +211,13 @@ mod tests {
206211
#[test]
207212
fn type_id_hasher() {
208213
use core::any::TypeId;
209-
use core::hash::Hash;
214+
use core::hash::Hash as _;
210215
fn verify_hashing_with(type_id: TypeId) {
211216
let mut hasher = TypeIdHasher::default();
212217
type_id.hash(&mut hasher);
213-
// SAFETY: u64 is valid for all bit patterns.
214-
let _ = hasher.finish();
218+
_ = hasher.finish();
215219
}
216-
// Pick a variety of types, just to demonstrate its all sane. Normal, zero-sized, unsized, &c.
220+
// Pick a variety of types, just to demonstrate it's all sane. Normal, zero-sized, unsized, &c.
217221
verify_hashing_with(TypeId::of::<usize>());
218222
verify_hashing_with(TypeId::of::<()>());
219223
verify_hashing_with(TypeId::of::<str>());
@@ -225,34 +229,34 @@ mod tests {
225229
/// Methods for downcasting from an `Any`-like trait object.
226230
///
227231
/// This should only be implemented on trait objects for subtraits of `Any`, though you can
228-
/// implement it for other types and it’ll work fine, so long as your implementation is correct.
232+
/// implement it for other types and it will work fine, so long as your implementation is correct.
229233
pub trait Downcast {
230234
/// Gets the `TypeId` of `self`.
231235
fn type_id(&self) -> TypeId;
232236

233237
// Note the bound through these downcast methods is 'static, rather than the inexpressible
234238
// concept of Self-but-as-a-trait (where Self is `dyn Trait`). This is sufficient, exceeding
235-
// TypeIds requirements. Sure, you *can* do CloneAny.downcast_unchecked::<NotClone>() and the
236-
// type system wont protect you, but that doesnt introduce any unsafety: the method is
239+
// TypeId's requirements. Sure, you *can* do CloneAny.downcast_unchecked::<NotClone>() and the
240+
// type system won't protect you, but that doesn't introduce any unsafety: the method is
237241
// already unsafe because you can specify the wrong type, and if this were exposing safe
238242
// downcasting, CloneAny.downcast::<NotClone>() would just return an error, which is just as
239243
// correct.
240244
//
241-
// Now in theory we could also add T: ?Sized, but that doesnt play nicely with the common
242-
// implementation, so Im doing without it.
245+
// Now in theory we could also add T: ?Sized, but that doesn't play nicely with the common
246+
// implementation, so I'm doing without it.
243247

244248
/// Downcast from `&Any` to `&T`, without checking the type matches.
245249
///
246250
/// # Safety
247251
///
248-
/// The caller must ensure that `T` matches the trait object, on pain of *undefined behaviour*.
252+
/// The caller must ensure that `T` matches the trait object, on pain of *undefined behavior*.
249253
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T;
250254

251255
/// Downcast from `&mut Any` to `&mut T`, without checking the type matches.
252256
///
253257
/// # Safety
254258
///
255-
/// The caller must ensure that `T` matches the trait object, on pain of *undefined behaviour*.
259+
/// The caller must ensure that `T` matches the trait object, on pain of *undefined behavior*.
256260
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T;
257261
}
258262

@@ -272,12 +276,12 @@ macro_rules! implement {
272276

273277
#[inline]
274278
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
275-
unsafe { &*(self as *const Self as *const T) }
279+
unsafe { &*std::ptr::from_ref::<Self>(self).cast::<T>() }
276280
}
277281

278282
#[inline]
279283
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
280-
unsafe { &mut *(self as *mut Self as *mut T) }
284+
unsafe { &mut *std::ptr::from_mut::<Self>(self).cast::<T>() }
281285
}
282286
}
283287

0 commit comments

Comments
 (0)