rhai/src/ast/namespace.rs

154 lines
3.8 KiB
Rust
Raw Normal View History

2022-01-29 11:09:43 +08:00
//! Namespace reference type.
#![cfg(not(feature = "no_module"))]
use crate::ast::Ident;
use crate::tokenizer::Token;
2022-02-10 17:55:32 +08:00
use crate::{Position, StaticVec};
2022-01-29 11:09:43 +08:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
fmt,
num::NonZeroUsize,
ops::{Deref, DerefMut},
};
2022-02-09 22:40:13 +08:00
/// _(internals)_ A chain of [module][crate::Module] names to namespace-qualify a variable or function call.
2022-01-29 11:09:43 +08:00
/// Exported under the `internals` feature only.
///
/// Not available under `no_module`.
///
2022-05-19 14:41:48 +08:00
/// A [`u64`] offset to the current stack of imported [modules][crate::Module] in the
/// [global runtime state][crate::GlobalRuntimeState] is cached for quick search purposes.
2022-01-29 11:09:43 +08:00
///
/// A [`StaticVec`] is used because the vast majority of namespace-qualified access contains only
/// one level, and it is wasteful to always allocate a [`Vec`] with one element.
#[derive(Clone, Eq, PartialEq, Default, Hash)]
pub struct Namespace {
path: StaticVec<Ident>,
2022-03-03 13:02:57 +08:00
index: Option<NonZeroUsize>,
2022-01-29 11:09:43 +08:00
}
impl fmt::Debug for Namespace {
2022-09-27 08:52:39 +08:00
#[cold]
#[inline(never)]
2022-01-29 11:09:43 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022-03-05 17:57:23 +08:00
if self.is_empty() {
return f.write_str("NONE");
}
2022-01-29 11:09:43 +08:00
if let Some(index) = self.index {
write!(f, "{} -> ", index)?;
}
f.write_str(
&self
.path
.iter()
2022-07-27 18:04:59 +08:00
.map(Ident::as_str)
2022-01-29 11:09:43 +08:00
.collect::<StaticVec<_>>()
.join(Token::DoubleColon.literal_syntax()),
)
}
}
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022-03-05 17:57:23 +08:00
if self.is_empty() {
return Ok(());
}
2022-01-29 11:09:43 +08:00
f.write_str(
&self
.path
.iter()
2022-07-27 18:04:59 +08:00
.map(Ident::as_str)
2022-01-29 11:09:43 +08:00
.collect::<StaticVec<_>>()
.join(Token::DoubleColon.literal_syntax()),
)
}
}
impl Deref for Namespace {
type Target = StaticVec<Ident>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.path
}
}
impl DerefMut for Namespace {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.path
}
}
impl From<Vec<Ident>> for Namespace {
2022-09-27 23:04:22 +08:00
#[inline]
2022-01-29 11:09:43 +08:00
fn from(mut path: Vec<Ident>) -> Self {
path.shrink_to_fit();
Self {
index: None,
path: path.into(),
}
}
}
impl From<StaticVec<Ident>> for Namespace {
2022-09-27 23:04:22 +08:00
#[inline]
2022-01-29 11:09:43 +08:00
fn from(mut path: StaticVec<Ident>) -> Self {
path.shrink_to_fit();
Self { index: None, path }
}
}
impl Namespace {
2022-03-05 17:57:23 +08:00
/// Constant for no namespace.
pub const NONE: Self = Self {
index: None,
path: StaticVec::new_const(),
};
2022-01-29 11:09:43 +08:00
/// Create a new [`Namespace`].
#[inline(always)]
#[must_use]
2022-03-05 17:57:23 +08:00
pub fn new(root: impl Into<Ident>) -> Self {
let mut path = StaticVec::new_const();
path.push(root.into());
Self { index: None, path }
2022-01-29 11:09:43 +08:00
}
/// Get the [`Scope`][crate::Scope] index offset.
#[inline(always)]
#[must_use]
pub(crate) const fn index(&self) -> Option<NonZeroUsize> {
self.index
}
/// Set the [`Scope`][crate::Scope] index offset.
#[inline(always)]
pub(crate) fn set_index(&mut self, index: Option<NonZeroUsize>) {
2022-07-27 18:04:59 +08:00
self.index = index;
2022-01-29 11:09:43 +08:00
}
2022-02-25 11:42:59 +08:00
/// Get the [position][Position] of this [`Namespace`].
2022-02-10 17:55:32 +08:00
///
/// # Panics
///
/// Panics if the path is empty.
2022-03-03 13:02:57 +08:00
#[inline(always)]
#[must_use]
2022-02-10 17:55:32 +08:00
pub fn position(&self) -> Position {
self.path[0].pos
}
2022-03-03 13:02:57 +08:00
/// Get the first path segment of this [`Namespace`].
///
/// # Panics
///
/// Panics if the path is empty.
#[inline(always)]
#[must_use]
pub fn root(&self) -> &str {
&self.path[0].name
}
2022-01-29 11:09:43 +08:00
}