rune_core::item

Struct ItemBuf

Source
pub struct ItemBuf<A: Allocator = Global> { /* private fields */ }
Expand description

The name of an item in the Rune Language.

This is made up of a collection of strings, like ["foo", "bar"]. This is indicated in rune as foo::bar.

An item can also belongs to a crate, which in rune could be indicated as ::crate::foo::bar. These items must be constructed using ItemBuf::with_crate.

Items are inlined if they are smaller than 32 bytes.

§Panics

The max length of a string component is is 2**14 = 16384. Attempting to add a string larger than that will panic. This also constitutes the maximum number of nested sibling components that can exist in a single source file since they all use anonymous identifiers.

§Component encoding

The following details internal implementation details of an Item, and is not exposed through its API. It is provided here in case you need to work with the internal of an item.

A single component is encoded as:

  • A two byte tag as a u16 in native endianess, indicating its type (least significant 2 bits) and data (most significant 14 bits).
  • If the type is a STRING, the data is treated as the length of the string. Any other type this the data is treated as the numeric id of the component.
  • If the type is a STRING, the tag is repeated at the end of it to allow for seeking backwards. This is not the case for other types. Since they are fixed size its not necessary.

So all in all, a string is encoded as this where the d part indicates the length of the string:

dddddddd ddddddtt *string content* dddddddd ddddddtt

And any other component is just the two bytes where the d part makes up a numerical component:

dddddddd ddddddtt

Implementations§

Source§

impl<A: Allocator> ItemBuf<A>

Source

pub fn push<C>(&mut self, c: C) -> Result<()>
where C: IntoComponent,

Push the given component to the current item.

Source

pub fn pop(&mut self) -> bool

Pop a the tail component, returning true if there was something to pop.

Source

pub fn extend<I>(&mut self, i: I) -> Result<()>

Extend the current item with an iterator.

Source

pub fn clear(&mut self)

Clear the current item.

Source§

impl ItemBuf

Source

pub const fn new() -> Self

Construct a new empty item.

§Examples
use rune::ItemBuf;

let item = ItemBuf::new();
let mut it = item.iter();

assert_eq!(it.next(), None);
Source

pub fn with_item(iter: impl IntoIterator<Item: IntoComponent>) -> Result<Self>

Construct a new item with the given path.

§Examples
use rune::ItemBuf;
use rune::item::ComponentRef;

let item = ItemBuf::with_item(["foo", "bar"])?;
let mut it = item.iter();

assert_eq!(it.next(), Some(ComponentRef::Str("foo")));
assert_eq!(it.next(), Some(ComponentRef::Str("bar")));
assert_eq!(it.next(), None);
Source

pub fn with_crate(name: &str) -> Result<Self>

Construct item for a crate.

§Examples
use rune::ItemBuf;
use rune::item::ComponentRef;

let mut item = ItemBuf::with_crate("std")?;
item.push("foo");
assert_eq!(item.as_crate(), Some("std"));

let mut it = item.iter();
assert_eq!(it.next(), Some(ComponentRef::Crate("std")));
assert_eq!(it.next(), Some(ComponentRef::Str("foo")));
assert_eq!(it.next(), None);
Source

pub fn with_crate_item<I>(name: &str, iter: I) -> Result<Self>

Create a crated item with the given name.

§Examples
use rune::ItemBuf;
use rune::item::ComponentRef;

let item = ItemBuf::with_crate_item("std", ["option"])?;
assert_eq!(item.as_crate(), Some("std"));

let mut it = item.iter();
assert_eq!(it.next(), Some(ComponentRef::Crate("std")));
assert_eq!(it.next(), Some(ComponentRef::Str("option")));
assert_eq!(it.next(), None);

Methods from Deref<Target = Item>§

Source

pub fn as_bytes(&self) -> &[u8]

Return the underlying byte representation of the Item.

§Examples
use rune::{Item, ItemBuf};

assert_eq!(Item::new().as_bytes(), b"");

let item = ItemBuf::with_item(["foo", "bar"])?;
assert_eq!(item.as_bytes(), b"\x0d\0foo\x0d\0\x0d\0bar\x0d\0");
Source

pub fn as_crate(&self) -> Option<&str>

Get the crate corresponding to the item.

§Examples
use rune::ItemBuf;

let item = ItemBuf::with_crate("std")?;
assert_eq!(item.as_crate(), Some("std"));

let item = ItemBuf::with_item(["local"])?;
assert_eq!(item.as_crate(), None);
Source

pub fn first(&self) -> Option<ComponentRef<'_>>

Access the first component of this item.

§Examples
use rune::ItemBuf;
use rune::item::ComponentRef;

let item = ItemBuf::with_item(["foo", "bar"])?;
assert_eq!(item.first(), Some(ComponentRef::Str("foo")));
Source

pub fn is_empty(&self) -> bool

Check if the item is empty.

§Examples
use rune::ItemBuf;

let item = ItemBuf::new();
assert!(item.is_empty());

let item = ItemBuf::with_crate("std")?;
assert!(!item.is_empty());
Source

pub fn as_vec(&self) -> Result<Vec<Component>>

Construct a new vector from the current item.

Source

pub fn as_local(&self) -> Option<&str>

If the item only contains one element, return that element.

Source

pub fn join( &self, other: impl IntoIterator<Item: IntoComponent>, ) -> Result<ItemBuf>

Return an owned and joined variant of this item.

§Examples
use rune::Item;
use rune::item::ComponentRef;

let item = Item::new();
assert!(item.is_empty());

let item2 = item.join(["hello", "world"])?;
assert_eq!(item2.first(), Some(ComponentRef::Str("hello")));
assert_eq!(item2.last(), Some(ComponentRef::Str("world")));
Source

pub fn extended<C>(&self, part: C) -> Result<ItemBuf>
where C: IntoComponent,

Return an owned and extended variant of this item.

§Examples
use rune::Item;
use rune::item::ComponentRef;

let item = Item::new();
assert!(item.is_empty());

let item2 = item.extended("hello")?;
assert_eq!(item2.first(), Some(ComponentRef::Str("hello")));
Source

pub fn last(&self) -> Option<ComponentRef<'_>>

Access the last component in the path.

Source

pub fn base_name(&self) -> Option<&str>

Access the base name of the item if available.

The base name is the last string component of the item.

Source

pub fn iter(&self) -> Iter<'_>

An iterator over the Components that constitute this item.

§Examples
use rune::ItemBuf;
use rune::item::{ComponentRef, IntoComponent};

let mut item = ItemBuf::new();

item.push("start")?;
item.push(ComponentRef::Id(1))?;
item.push(ComponentRef::Id(2))?;
item.push("middle")?;
item.push(ComponentRef::Id(3))?;
item.push("end")?;

let mut it = item.iter();

assert_eq!(it.next(), Some("start".as_component_ref()));
assert_eq!(it.next(), Some(ComponentRef::Id(1)));
assert_eq!(it.next(), Some(ComponentRef::Id(2)));
assert_eq!(it.next(), Some("middle".as_component_ref()));
assert_eq!(it.next(), Some(ComponentRef::Id(3)));
assert_eq!(it.next(), Some("end".as_component_ref()));
assert_eq!(it.next(), None);

assert!(!item.is_empty());
Source

pub fn starts_with<U>(&self, other: U) -> bool
where U: AsRef<Item>,

Test if current item starts with another.

Source

pub fn is_super_of<U>(&self, other: U, n: usize) -> bool
where U: AsRef<Item>,

Test if current is immediate super of other.

§Examples
use rune::{Item, ItemBuf};

assert!(Item::new().is_super_of(Item::new(), 1));
assert!(!ItemBuf::with_item(["a"])?.is_super_of(Item::new(), 1));

assert!(!ItemBuf::with_item(["a", "b"])?.is_super_of(ItemBuf::with_item(["a"])?, 1));
assert!(ItemBuf::with_item(["a", "b"])?.is_super_of(ItemBuf::with_item(["a", "b"])?, 1));
assert!(!ItemBuf::with_item(["a"])?.is_super_of(ItemBuf::with_item(["a", "b", "c"])?, 1));
Source

pub fn ancestry<U>(&self, other: U) -> Result<(ItemBuf, ItemBuf)>
where U: AsRef<Item>,

Get the ancestry of one module to another.

This returns three things:

  • The shared prefix between the current and the other path.
  • The suffix to get to the other path from the shared prefix.
§Examples
use rune::{Item, ItemBuf};

assert_eq!(
    (ItemBuf::new(), ItemBuf::new()),
    Item::new().ancestry(Item::new())?
);

assert_eq!(
    (ItemBuf::new(), ItemBuf::with_item(["a"])?),
    Item::new().ancestry(ItemBuf::with_item(["a"])?)?
);

assert_eq!(
    (ItemBuf::new(), ItemBuf::with_item(["a", "b"])?),
    Item::new().ancestry(ItemBuf::with_item(["a", "b"])?)?
);

assert_eq!(
    (ItemBuf::with_item(["a"])?, ItemBuf::with_item(["b"])?),
    ItemBuf::with_item(["a", "c"])?.ancestry(ItemBuf::with_item(["a", "b"])?)?
);

assert_eq!(
    (ItemBuf::with_item(["a", "b"])?, ItemBuf::with_item(["d", "e"])?),
    ItemBuf::with_item(["a", "b", "c"])?.ancestry(ItemBuf::with_item(["a", "b", "d", "e"])?)?
);
Source

pub fn parent(&self) -> Option<&Item>

Get the parent item for the current item.

§Examples
use rune::ItemBuf;

let item = ItemBuf::with_item(["foo", "bar", "baz"])?;
let item2 = ItemBuf::with_item(["foo", "bar"])?;

assert_eq!(item.parent(), Some(&*item2));
Source

pub fn unqalified(&self) -> Unqalified<'_>

Display an unqalified variant of the item which does not include :: if a crate is present.

Trait Implementations§

Source§

impl<A: Allocator> AsRef<Item> for ItemBuf<A>

Source§

fn as_ref(&self) -> &Item

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<A: Allocator> Borrow<Item> for ItemBuf<A>

Source§

fn borrow(&self) -> &Item

Immutably borrows from an owned value. Read more
Source§

impl<A: Allocator> Debug for ItemBuf<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A> Default for ItemBuf<A>
where A: Default + Allocator,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A: Allocator> Deref for ItemBuf<A>

Source§

type Target = Item

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de, A> Deserialize<'de> for ItemBuf<A>
where A: Default + Allocator,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<A: Allocator> Display for ItemBuf<A>

Format implementation for an ItemBuf, defers to Item.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A> FromStr for ItemBuf<A>
where A: Default + Allocator,

Source§

type Err = FromStrError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<A: Allocator> Hash for ItemBuf<A>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, A: Allocator> IntoIterator for &'a ItemBuf<A>

Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = ComponentRef<'a>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<A: Allocator> Ord for ItemBuf<A>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<A: Allocator> PartialEq<&Item> for ItemBuf<A>

Source§

fn eq(&self, other: &&Item) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialEq<Item> for &ItemBuf<A>

Source§

fn eq(&self, other: &Item) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialEq<Item> for ItemBuf<A>

Source§

fn eq(&self, other: &Item) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<ItemBuf> for &Item

Source§

fn eq(&self, other: &ItemBuf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<ItemBuf> for Item

Source§

fn eq(&self, other: &ItemBuf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<ItemBuf> for Iter<'_>

Source§

fn eq(&self, other: &ItemBuf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialEq<Iter<'_>> for &ItemBuf<A>

Source§

fn eq(&self, other: &Iter<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialEq<Iter<'_>> for ItemBuf<A>

Source§

fn eq(&self, other: &Iter<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialEq for ItemBuf<A>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Allocator> PartialOrd for ItemBuf<A>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<A: Allocator> Serialize for ItemBuf<A>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<A: Allocator + Clone> TryClone for ItemBuf<A>

Source§

fn try_clone(&self) -> Result<Self>

Try to clone the current value, raising an allocation error if it’s unsuccessful.
Source§

fn try_clone_from(&mut self, source: &Self) -> Result<(), Error>

Performs copy-assignment from source. Read more
Source§

impl<C, A: Allocator> TryFromIteratorIn<C, A> for ItemBuf<A>
where C: IntoComponent,

Source§

fn try_from_iter_in<T: IntoIterator<Item = C>>( iter: T, alloc: A, ) -> Result<Self>

Creates a value from an iterator within an allocator.
Source§

impl<A: Allocator> Eq for ItemBuf<A>

Auto Trait Implementations§

§

impl<A> Freeze for ItemBuf<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for ItemBuf<A>
where A: RefUnwindSafe,

§

impl<A> Send for ItemBuf<A>
where A: Send,

§

impl<A> Sync for ItemBuf<A>
where A: Sync,

§

impl<A> Unpin for ItemBuf<A>
where A: Unpin,

§

impl<A> UnwindSafe for ItemBuf<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryFromIterator<T> for U

Source§

fn try_from_iter<I>(iter: I) -> Result<U, Error>
where I: IntoIterator<Item = T>,

Creates a value from an iterator within an allocator.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TryToOwned for T
where T: TryClone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn try_to_owned(&self) -> Result<T, Error>

Creates owned data from borrowed data, usually by cloning. Read more
Source§

impl<T> TryToString for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, Error>

Converts the given value to a String. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,