|
1 | 1 | //! File and span related types.
|
2 | 2 | use std::fmt::{self, Write};
|
3 | 3 |
|
| 4 | +#[cfg(feature = "ra-salsa")] |
4 | 5 | use ra_salsa::InternId;
|
5 | 6 |
|
6 | 7 | mod ast_id;
|
@@ -355,3 +356,71 @@ impl HirFileId {
|
355 | 356 | }
|
356 | 357 | }
|
357 | 358 | }
|
| 359 | + |
| 360 | +#[cfg(not(feature = "ra-salsa"))] |
| 361 | +mod intern_id_proxy { |
| 362 | + use std::fmt; |
| 363 | + use std::num::NonZeroU32; |
| 364 | + |
| 365 | + pub(super) struct InternId { |
| 366 | + value: NonZeroU32, |
| 367 | + } |
| 368 | + |
| 369 | + impl InternId { |
| 370 | + pub(super) const MAX: u32 = 0xFFFF_FF00; |
| 371 | + |
| 372 | + pub(super) const unsafe fn new_unchecked(value: u32) -> Self { |
| 373 | + debug_assert!(value < InternId::MAX); |
| 374 | + let value = unsafe { NonZeroU32::new_unchecked(value + 1) }; |
| 375 | + InternId { value } |
| 376 | + } |
| 377 | + |
| 378 | + pub(super) fn as_u32(self) -> u32 { |
| 379 | + self.value.get() - 1 |
| 380 | + } |
| 381 | + |
| 382 | + pub(super) fn as_usize(self) -> usize { |
| 383 | + self.as_u32() as usize |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + impl From<InternId> for u32 { |
| 388 | + fn from(raw: InternId) -> u32 { |
| 389 | + raw.as_u32() |
| 390 | + } |
| 391 | + } |
| 392 | + |
| 393 | + impl From<InternId> for usize { |
| 394 | + fn from(raw: InternId) -> usize { |
| 395 | + raw.as_usize() |
| 396 | + } |
| 397 | + } |
| 398 | + |
| 399 | + impl From<u32> for InternId { |
| 400 | + fn from(id: u32) -> InternId { |
| 401 | + assert!(id < InternId::MAX); |
| 402 | + unsafe { InternId::new_unchecked(id) } |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + impl From<usize> for InternId { |
| 407 | + fn from(id: usize) -> InternId { |
| 408 | + assert!(id < (InternId::MAX as usize)); |
| 409 | + unsafe { InternId::new_unchecked(id as u32) } |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + impl fmt::Debug for InternId { |
| 414 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 415 | + self.as_usize().fmt(f) |
| 416 | + } |
| 417 | + } |
| 418 | + |
| 419 | + impl fmt::Display for InternId { |
| 420 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 421 | + self.as_usize().fmt(f) |
| 422 | + } |
| 423 | + } |
| 424 | +} |
| 425 | +#[cfg(not(feature = "ra-salsa"))] |
| 426 | +use intern_id_proxy::InternId; |
0 commit comments