Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

cranelift-entity: remove unused `get_raw_mut` and `into_boxed_slice` (#15159)

Wei-Lun Li55a53219ee40
diff --git a/cranelift/entity/src/boxed_slice.rs b/cranelift/entity/src/boxed_slice.rs
deleted file mode 100644
index 75daa30068..0000000000
--- a/cranelift/entity/src/boxed_slice.rs
+++ /dev/null
@@ -1,316 +0,0 @@
-//! Boxed slices for `PrimaryMap`.
-
-use crate::EntityRef;
-use crate::iter::{Iter, IterMut};
-use crate::keys::Keys;
-use alloc::boxed::Box;
-use core::marker::PhantomData;
-use core::ops::{Index, IndexMut};
-use core::slice;
-
-/// A slice mapping `K -> V` allocating dense entity references.
-///
-/// The `BoxedSlice` data structure uses the dense index space to implement a map with a boxed
-/// slice.
-#[derive(Debug, Clone)]
-pub struct BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- elems: Box<[V]>,
- unused: PhantomData<K>,
-}
-
-impl<K, V> BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- /// Create a new slice from a raw pointer. A safer way to create slices is
- /// to use `PrimaryMap::into_boxed_slice()`.
- ///
- /// # Safety
- ///
- /// This relies on `raw` pointing to a valid slice of `V`s.
- pub unsafe fn from_raw(raw: *mut [V]) -> Self {
- Self {
- elems: unsafe { Box::from_raw(raw) },
- unused: PhantomData,
- }
- }
-
- /// Check if `k` is a valid key in the map.
- pub fn is_valid(&self, k: K) -> bool {
- k.index() < self.elems.len()
- }
-
- /// Get the element at `k` if it exists.
- pub fn get(&self, k: K) -> Option<&V> {
- self.elems.get(k.index())
- }
-
- /// Get the element at `k` if it exists, mutable version.
- pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
- self.elems.get_mut(k.index())
- }
-
- /// Is this map completely empty?
- pub fn is_empty(&self) -> bool {
- self.elems.is_empty()
- }
-
- /// Get the total number of entity references created.
- pub fn len(&self) -> usize {
- self.elems.len()
- }
-
- /// Iterate over all the keys in this map.
- pub fn keys(&self) -> Keys<K> {
- Keys::with_len(self.elems.len())
- }
-
- /// Iterate over all the values in this map.
- pub fn values(&self) -> slice::Iter<'_, V> {
- self.elems.iter()
- }
-
- /// Iterate over all the values in this map, mutable edition.
- pub fn values_mut(&mut self) -> slice::IterMut<'_, V> {
- self.elems.iter_mut()
- }
-
- /// Iterate over all the keys and values in this map.
- pub fn iter(&self) -> Iter<'_, K, V> {
- Iter::new(self.elems.iter())
- }
-
- /// Iterate over all the keys and values in this map, mutable edition.
- pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
- IterMut::new(self.elems.iter_mut())
- }
-
- /// Returns the last element that was inserted in the map.
- pub fn last(&self) -> Option<&V> {
- self.elems.last()
- }
-}
-
-/// Immutable indexing into a `BoxedSlice`.
-/// The indexed value must be in the map.
-impl<K, V> Index<K> for BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- type Output = V;
-
- fn index(&self, k: K) -> &V {
- &self.elems[k.index()]
- }
-}
-
-/// Mutable indexing into a `BoxedSlice`.
-impl<K, V> IndexMut<K> for BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- fn index_mut(&mut self, k: K) -> &mut V {
- &mut self.elems[k.index()]
- }
-}
-
-impl<'a, K, V> IntoIterator for &'a BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- type Item = (K, &'a V);
- type IntoIter = Iter<'a, K, V>;
-
- fn into_iter(self) -> Self::IntoIter {
- Iter::new(self.elems.iter())
- }
-}
-
-impl<'a, K, V> IntoIterator for &'a mut BoxedSlice<K, V>
-where
- K: EntityRef,
-{
- type Item = (K, &'a mut V);
- type IntoIter = IterMut<'a, K, V>;
-
- fn into_iter(self) -> Self::IntoIter {
- IterMut::new(self.elems.iter_mut())
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::primary::PrimaryMap;
- use alloc::vec::Vec;
-
- // `EntityRef` impl for testing.
- #[derive(Clone, Copy, Debug, PartialEq, Eq)]
- struct E(u32);
-
- impl EntityRef for E {
- fn new(i: usize) -> Self {
- E(i as u32)
- }
- fn index(self) -> usize {
- self.0 as usize
- }
- }
-
- #[test]
- fn basic() {
- let r0 = E(0);
- let r1 = E(1);
- let p = PrimaryMap::<E, isize>::new();
- let m = p.into_boxed_slice();
-
- let v: Vec<E> = m.keys().collect();
- assert_eq!(v, []);
-
- assert!(!m.is_valid(r0));
- assert!(!m.is_valid(r1));
- }
-
- #[test]
- fn iter() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let mut m = p.into_boxed_slice();
-
- let mut i = 0;
- for (key, value) in &m {
- assert_eq!(key.index(), i);
- match i {
- 0 => assert_eq!(*value, 12),
- 1 => assert_eq!(*value, 33),
- _ => panic!(),
- }
- i += 1;
- }
- i = 0;
- for (key_mut, value_mut) in m.iter_mut() {
- assert_eq!(key_mut.index(), i);
- match i {
- 0 => assert_eq!(*value_mut, 12),
- 1 => assert_eq!(*value_mut, 33),
- _ => panic!(),
- }
- i += 1;
- }
- }
-
- #[test]
- fn iter_rev() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let mut m = p.into_boxed_slice();
-
- let mut i = 2;
- for (key, value) in m.iter().rev() {
- i -= 1;
- assert_eq!(key.index(), i);
- match i {
- 0 => assert_eq!(*value, 12),
- 1 => assert_eq!(*value, 33),
- _ => panic!(),
- }
- }
-
- i = 2;
- for (key, value) in m.iter_mut().rev() {
- i -= 1;
- assert_eq!(key.index(), i);
- match i {
- 0 => assert_eq!(*value, 12),
- 1 => assert_eq!(*value, 33),
- _ => panic!(),
- }
- }
- }
- #[test]
- fn keys() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let m = p.into_boxed_slice();
-
- let mut i = 0;
- for key in m.keys() {
- assert_eq!(key.index(), i);
- i += 1;
- }
- }
-
- #[test]
- fn keys_rev() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let m = p.into_boxed_slice();
-
- let mut i = 2;
- for key in m.keys().rev() {
- i -= 1;
- assert_eq!(key.index(), i);
- }
- }
-
- #[test]
- fn values() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let mut m = p.into_boxed_slice();
-
- let mut i = 0;
- for value in m.values() {
- match i {
- 0 => assert_eq!(*value, 12),
- 1 => assert_eq!(*value, 33),
- _ => panic!(),
- }
- i += 1;
- }
- i = 0;
- for value_mut in m.values_mut() {
- match i {
- 0 => assert_eq!(*value_mut, 12),
- 1 => assert_eq!(*value_mut, 33),
- _ => panic!(),
- }
- i += 1;
- }
- }
-
- #[test]
- fn values_rev() {
- let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
- p.push(12);
- p.push(33);
- let mut m = p.into_boxed_slice();
-
- let mut i = 2;
- for value in m.values().rev() {
- i -= 1;
- match i {
- 0 => assert_eq!(*value, 12),
- 1 => assert_eq!(*value, 33),
- _ => panic!(),
- }
- }
- i = 2;
- for value_mut in m.values_mut().rev() {
- i -= 1;
- match i {
- 0 => assert_eq!(*value_mut, 12),
- 1 => assert_eq!(*value_mut, 33),
- _ => panic!(),
- }
- }
- }
-}
diff --git a/cranelift/entity/src/lib.rs b/cranelift/entity/src/lib.rs
index 9a7ac7d0ee..b7d2a6674c 100644
--- a/cranelift/entity/src/lib.rs
+++ b/cranelift/entity/src/lib.rs
@@ -270,7 +270,6 @@ macro_rules! entity_impl {
pub mod packed_option;
-mod boxed_slice;
mod iter;
mod keys;
mod list;
@@ -279,7 +278,6 @@ mod primary;
mod set;
mod sparse;
-pub use self::boxed_slice::BoxedSlice;
pub use self::iter::{IntoIter, Iter, IterMut};
pub use self::keys::Keys;
pub use self::list::{EntityList, ListPool};
diff --git a/cranelift/entity/src/primary.rs b/cranelift/entity/src/primary.rs
index a39953a5d9..02d138d5ae 100644
--- a/cranelift/entity/src/primary.rs
+++ b/cranelift/entity/src/primary.rs
@@ -1,9 +1,7 @@
//! Densely numbered entity references as mapping keys.
use crate::EntityRef;
-use crate::boxed_slice::BoxedSlice;
use crate::iter::{IntoIter, Iter, IterMut};
use crate::keys::Keys;
-use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;
@@ -26,8 +24,7 @@ use wasmtime_core::error::OutOfMemory;
/// Note that `PrimaryMap` doesn't implement `Deref` or `DerefMut`, which would allow
/// `&PrimaryMap<K, V>` to convert to `&[V]`. One of the main advantages of `PrimaryMap` is
/// that it only allows indexing with the distinct `EntityRef` key type, so converting to a
-/// plain slice would make it easier to use incorrectly. To make a slice of a `PrimaryMap`, use
-/// `into_boxed_slice`.
+/// plain slice would make it easier to use incorrectly.
#[derive(Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct PrimaryMap<K, V>
@@ -185,11 +182,6 @@ where
self.elems.shrink_to_fit()
}
- /// Consumes this `PrimaryMap` and produces a `BoxedSlice`.
- pub fn into_boxed_slice(self) -> BoxedSlice<K, V> {
- unsafe { BoxedSlice::<K, V>::from_raw(Box::<[V]>::into_raw(self.elems.into_boxed_slice())) }
- }
-
/// Returns mutable references to many elements at once.
///
/// Returns an error if an element does not exist, or if the same key was passed more than
@@ -222,25 +214,6 @@ where
.map(|i| K::new(i))
.map_err(|i| K::new(i))
}
-
- /// Analog of `get_raw` except that a raw pointer is returned rather than a
- /// mutable reference.
- ///
- /// The default accessors of items in [`PrimaryMap`] will invalidate all
- /// previous borrows obtained from the map according to miri. This function
- /// can be used to acquire a pointer and then subsequently acquire a second
- /// pointer later on without invalidating the first one. In other words
- /// this is only here to help borrow two elements simultaneously with miri.
- pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {
- if k.index() < self.elems.len() {
- // SAFETY: the `add` function requires that the index is in-bounds
- // with respect to the allocation which is satisfied here due to
- // the bounds-check above.
- unsafe { Some(self.elems.as_mut_ptr().add(k.index())) }
- } else {
- None
- }
- }
}
impl<K, V> Default for PrimaryMap<K, V>
diff --git a/crates/environ/src/collections/primary_map.rs b/crates/environ/src/collections/primary_map.rs
index 3467fe5667..c5438b23d9 100644
--- a/crates/environ/src/collections/primary_map.rs
+++ b/crates/environ/src/collections/primary_map.rs
@@ -211,11 +211,6 @@ where
{
self.inner.binary_search_values_by_key(b, f)
}
-
- /// Same as [`cranelift_entity::PrimaryMap::get_raw_mut`].
- pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {
- self.inner.get_raw_mut(k)
- }
}
impl<K, V> TryExtend<V> for TryPrimaryMap<K, V>