s/RawId/InternId/

This commit is contained in:
Niko Matsakis 2019-04-03 11:00:59 -03:00
parent 40d0c8d21a
commit 74294f71f3
5 changed files with 77 additions and 73 deletions

View file

@ -6,61 +6,61 @@ use std::num::NonZeroU32;
/// devising. For more information about interned keys, see [the
/// interned key RFC][rfc].
///
/// # Creating a `RawId`
/// # Creating a `InternId`
//
/// RawId values can be constructed using the `From` impls,
/// InternId values can be constructed using the `From` impls,
/// which are implemented for `u32` and `usize`:
///
/// ```
/// # use salsa::RawId;
/// let raw_id1 = RawId::from(22_u32);
/// let raw_id2 = RawId::from(22_usize);
/// assert_eq!(raw_id1, raw_id2);
/// # use salsa::InternId;
/// let intern_id1 = InternId::from(22_u32);
/// let intern_id2 = InternId::from(22_usize);
/// assert_eq!(intern_id1, intern_id2);
/// ```
///
/// # Converting to a u32 or usize
///
/// Normally, there should be no need to access the underlying integer
/// in a `RawId`. But if you do need to do so, you can convert to a
/// in a `InternId`. But if you do need to do so, you can convert to a
/// `usize` using the `as_u32` or `as_usize` methods or the `From` impls.
///
/// ```
/// # use salsa::RawId;
/// let raw_id = RawId::from(22_u32);
/// let value = u32::from(raw_id);
/// # use salsa::InternId;
/// let intern_id = InternId::from(22_u32);
/// let value = u32::from(intern_id);
/// assert_eq!(value, 22);
/// ```
///
/// ## Illegal values
///
/// Be warned, however, that `RawId` values cannot be created from
/// Be warned, however, that `InternId` values cannot be created from
/// *arbitrary* values -- in particular large values greater than
/// `RawId::MAX` will panic. Those large values are reserved so that
/// `InternId::MAX` will panic. Those large values are reserved so that
/// the Rust compiler can use them as sentinel values, which means
/// that (for example) `Option<RawId>` is represented in a single
/// that (for example) `Option<InternId>` is represented in a single
/// word.
///
/// ```should_panic
/// # use salsa::RawId;
/// RawId::from(RawId::MAX);
/// # use salsa::InternId;
/// InternId::from(InternId::MAX);
/// ```
///
/// [rfc]: https://github.com/salsa-rs/salsa-rfcs/pull/2
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawId {
pub struct InternId {
value: NonZeroU32,
}
impl RawId {
/// The maximum allowed `RawId`. This value can grow between
impl InternId {
/// The maximum allowed `InternId`. This value can grow between
/// releases without affecting semver.
pub const MAX: u32 = 0xFFFF_FF00;
/// Creates a new RawId. Unsafe as `value` must be less than `MAX`
/// Creates a new InternId. Unsafe as `value` must be less than `MAX`
/// and this is not checked in release builds.
unsafe fn new_unchecked(value: u32) -> Self {
debug_assert!(value < RawId::MAX);
RawId {
debug_assert!(value < InternId::MAX);
InternId {
value: NonZeroU32::new_unchecked(value + 1),
}
}
@ -68,9 +68,9 @@ impl RawId {
/// Convert this raw-id into a u32 value.
///
/// ```
/// # use salsa::RawId;
/// let raw_id = RawId::from(22_u32);
/// let value = raw_id.as_usize();
/// # use salsa::InternId;
/// let intern_id = InternId::from(22_u32);
/// let value = intern_id.as_usize();
/// assert_eq!(value, 22);
/// ```
pub fn as_u32(self) -> u32 {
@ -80,9 +80,9 @@ impl RawId {
/// Convert this raw-id into a usize value.
///
/// ```
/// # use salsa::RawId;
/// let raw_id = RawId::from(22_u32);
/// let value = raw_id.as_usize();
/// # use salsa::InternId;
/// let intern_id = InternId::from(22_u32);
/// let value = intern_id.as_usize();
/// assert_eq!(value, 22);
/// ```
pub fn as_usize(self) -> usize {
@ -90,39 +90,39 @@ impl RawId {
}
}
impl From<RawId> for u32 {
fn from(raw: RawId) -> u32 {
impl From<InternId> for u32 {
fn from(raw: InternId) -> u32 {
raw.as_u32()
}
}
impl From<RawId> for usize {
fn from(raw: RawId) -> usize {
impl From<InternId> for usize {
fn from(raw: InternId) -> usize {
raw.as_usize()
}
}
impl From<u32> for RawId {
fn from(id: u32) -> RawId {
assert!(id < RawId::MAX);
unsafe { RawId::new_unchecked(id) }
impl From<u32> for InternId {
fn from(id: u32) -> InternId {
assert!(id < InternId::MAX);
unsafe { InternId::new_unchecked(id) }
}
}
impl From<usize> for RawId {
fn from(id: usize) -> RawId {
assert!(id < (RawId::MAX as usize));
unsafe { RawId::new_unchecked(id as u32) }
impl From<usize> for InternId {
fn from(id: usize) -> InternId {
assert!(id < (InternId::MAX as usize));
unsafe { InternId::new_unchecked(id as u32) }
}
}
impl fmt::Debug for RawId {
impl fmt::Debug for InternId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_usize().fmt(f)
}
}
impl fmt::Display for RawId {
impl fmt::Display for InternId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_usize().fmt(f)
}

View file

@ -1,9 +1,9 @@
use crate::debug::TableEntry;
use crate::intern_id::InternId;
use crate::plumbing::CycleDetected;
use crate::plumbing::HasQueryGroup;
use crate::plumbing::QueryStorageMassOps;
use crate::plumbing::QueryStorageOps;
use crate::raw_id::RawId;
use crate::runtime::ChangedAt;
use crate::runtime::Revision;
use crate::runtime::StampedValue;
@ -47,7 +47,7 @@ where
struct InternTables<K> {
/// Map from the key to the corresponding intern-index.
map: FxHashMap<K, RawId>,
map: FxHashMap<K, InternId>,
/// For each valid intern-index, stores the interned value. When
/// an interned value is GC'd, the entry is set to
@ -55,7 +55,7 @@ struct InternTables<K> {
values: Vec<InternValue<K>>,
/// Index of the first free intern-index, if any.
first_free: Option<RawId>,
first_free: Option<InternId>,
}
/// Trait implemented for the "key" that results from a
@ -63,18 +63,18 @@ struct InternTables<K> {
/// "newtype"'d `u32`.
pub trait InternKey {
/// Create an instance of the intern-key from a `u32` value.
fn from_raw_id(v: RawId) -> Self;
fn from_intern_id(v: InternId) -> Self;
/// Extract the `u32` with which the intern-key was created.
fn as_raw_id(&self) -> RawId;
fn as_intern_id(&self) -> InternId;
}
impl InternKey for RawId {
fn from_raw_id(v: RawId) -> RawId {
impl InternKey for InternId {
fn from_intern_id(v: InternId) -> InternId {
v
}
fn as_raw_id(&self) -> RawId {
fn as_intern_id(&self) -> InternId {
*self
}
}
@ -96,7 +96,7 @@ enum InternValue<K> {
},
/// Free-list -- the index is the next
Free { next: Option<RawId> },
Free { next: Option<InternId> },
}
impl<DB, Q> std::panic::RefUnwindSafe for InternedStorage<DB, Q>
@ -165,7 +165,7 @@ where
Q::Value: InternKey,
DB: Database,
{
fn intern_index(&self, db: &DB, key: &Q::Key) -> StampedValue<RawId> {
fn intern_index(&self, db: &DB, key: &Q::Key) -> StampedValue<InternId> {
if let Some(i) = self.intern_check(db, key) {
return i;
}
@ -208,7 +208,7 @@ where
let index = match tables.first_free {
None => {
let index = RawId::from(tables.values.len());
let index = InternId::from(tables.values.len());
tables.values.push(InternValue::Present {
value: owned_key2,
interned_at: revision_now,
@ -249,7 +249,7 @@ where
}
}
fn intern_check(&self, db: &DB, key: &Q::Key) -> Option<StampedValue<RawId>> {
fn intern_check(&self, db: &DB, key: &Q::Key) -> Option<StampedValue<InternId>> {
let revision_now = db.salsa_runtime().current_revision();
// First,
@ -316,7 +316,7 @@ where
fn lookup_value<R>(
&self,
db: &DB,
index: RawId,
index: InternId,
op: impl FnOnce(&Q::Key) -> R,
) -> StampedValue<R> {
let index = index.as_usize();
@ -399,7 +399,7 @@ where
db.salsa_runtime()
.report_query_read(database_key, changed_at);
Ok(<Q::Value>::from_raw_id(value))
Ok(<Q::Value>::from_intern_id(value))
}
fn maybe_changed_since(
@ -430,7 +430,9 @@ where
tables
.map
.iter()
.map(|(key, index)| TableEntry::new(key.clone(), Some(<Q::Value>::from_raw_id(*index))))
.map(|(key, index)| {
TableEntry::new(key.clone(), Some(<Q::Value>::from_intern_id(*index)))
})
.collect()
}
}
@ -509,7 +511,7 @@ where
key: &Q::Key,
database_key: &DB::DatabaseKey,
) -> Result<Q::Value, CycleDetected> {
let index = key.as_raw_id();
let index = key.as_intern_id();
let group_storage = <DB as HasQueryGroup<Q::Group>>::group_storage(db);
let interned_storage = IQ::query_storage(group_storage);
@ -529,7 +531,7 @@ where
key: &Q::Key,
_database_key: &DB::DatabaseKey,
) -> bool {
let index = key.as_raw_id();
let index = key.as_intern_id();
// NB. This will **panic** if `key` has been removed from the
// map, whereas you might expect it to return true in that
@ -575,7 +577,9 @@ where
tables
.map
.iter()
.map(|(key, index)| TableEntry::new(<Q::Key>::from_raw_id(*index), Some(key.clone())))
.map(|(key, index)| {
TableEntry::new(<Q::Key>::from_intern_id(*index), Some(key.clone()))
})
.collect()
}
}

View file

@ -10,8 +10,8 @@
mod derived;
mod input;
mod intern_id;
mod interned;
mod raw_id;
mod runtime;
pub mod debug;
@ -28,8 +28,8 @@ use derive_new::new;
use std::fmt::{self, Debug};
use std::hash::Hash;
pub use crate::intern_id::InternId;
pub use crate::interned::InternKey;
pub use crate::raw_id::RawId;
pub use crate::runtime::Runtime;
pub use crate::runtime::RuntimeId;

View file

@ -1,5 +1,5 @@
use crate::db;
use salsa::{Database, RawId, SweepStrategy};
use salsa::{Database, InternId, SweepStrategy};
/// Query group for tests for how interned keys interact with GC.
#[salsa::query_group(Intern)]
@ -10,20 +10,20 @@ pub(crate) trait InternDatabase {
/// Underlying interning query.
#[salsa::interned]
fn intern_str(&self, x: &'static str) -> RawId;
fn intern_str(&self, x: &'static str) -> InternId;
/// This just executes the intern query and returns the result.
fn repeat_intern1(&self, x: &'static str) -> RawId;
fn repeat_intern1(&self, x: &'static str) -> InternId;
/// Same as `repeat_intern1`. =)
fn repeat_intern2(&self, x: &'static str) -> RawId;
fn repeat_intern2(&self, x: &'static str) -> InternId;
}
fn repeat_intern1(db: &impl InternDatabase, x: &'static str) -> RawId {
fn repeat_intern1(db: &impl InternDatabase, x: &'static str) -> InternId {
db.intern_str(x)
}
fn repeat_intern2(db: &impl InternDatabase, x: &'static str) -> RawId {
fn repeat_intern2(db: &impl InternDatabase, x: &'static str) -> InternId {
db.intern_str(x)
}

View file

@ -1,6 +1,6 @@
//! Test that you can implement a query using a `dyn Trait` setup.
use salsa::RawId;
use salsa::InternId;
#[salsa::database(InternStorage)]
#[derive(Default)]
@ -25,24 +25,24 @@ impl salsa::ParallelDatabase for Database {
#[salsa::query_group(InternStorage)]
trait Intern {
#[salsa::interned]
fn intern1(&self, x: String) -> RawId;
fn intern1(&self, x: String) -> InternId;
#[salsa::interned]
fn intern2(&self, x: String, y: String) -> RawId;
fn intern2(&self, x: String, y: String) -> InternId;
#[salsa::interned]
fn intern_key(&self, x: String) -> InternKey;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct InternKey(RawId);
pub struct InternKey(InternId);
impl salsa::InternKey for InternKey {
fn from_raw_id(v: RawId) -> Self {
fn from_intern_id(v: InternId) -> Self {
InternKey(v)
}
fn as_raw_id(&self) -> RawId {
fn as_intern_id(&self) -> InternId {
self.0
}
}