Struct rune::compile::ItemBuf

source ·
pub struct ItemBuf<A = Global>
where A: Allocator,
{ /* 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> ItemBuf<A>
where A: Allocator,

source

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

Push the given component to the current item.

source

pub fn pop(&mut self) -> Result<Option<Component>, Error>

Push the given component to the current item.

source

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

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() -> ItemBuf

Construct a new empty item.

§Examples
use rune::compile::ItemBuf;

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

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

pub fn with_item<I>(iter: I) -> Result<ItemBuf, Error>

Construct a new item with the given path.

§Examples
use rune::compile::{ComponentRef, ItemBuf};

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<ItemBuf, Error>

Construct item for a crate.

§Examples
use rune::compile::{ComponentRef, ItemBuf};

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<ItemBuf, Error>

Create a crated item with the given name.

§Examples
use rune::compile::{ComponentRef, ItemBuf};

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::compile::{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::compile::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::compile::{ComponentRef, ItemBuf};

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::compile::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>, Error>

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<I>(&self, other: I) -> Result<ItemBuf, Error>

Return an owned and joined variant of this item.

§Examples
use rune::compile::{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, Error>
where C: IntoComponent,

Return an owned and extended variant of this item.

§Examples
use rune::compile::{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 iter(&self) -> Iter<'_>

An iterator over the Components that constitute this item.

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

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::compile::{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), Error>
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::compile::{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::compile::ItemBuf;

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

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

Trait Implementations§

source§

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

source§

fn as_ref(&self) -> &Item

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

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

source§

fn borrow(&self) -> &Item

Immutably borrows from an owned value. Read more
source§

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

source§

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

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

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

source§

fn default() -> ItemBuf<A>

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

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

§

type Target = Item

The resulting type after dereferencing.
source§

fn deref(&self) -> &<ItemBuf<A> as Deref>::Target

Dereferences the value.
source§

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

source§

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

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

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

Format implementation for an ItemBuf, defers to Item.

source§

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

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

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

§

type Err = FromStrError

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

fn from_str(s: &str) -> Result<ItemBuf<A>, <ItemBuf<A> as FromStr>::Err>

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

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

source§

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

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> IntoIterator for &'a ItemBuf<A>
where A: Allocator,

§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
§

type Item = ComponentRef<'a>

The type of the elements being iterated over.
source§

fn into_iter(self) -> <&'a ItemBuf<A> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

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

source§

fn cmp(&self, other: &ItemBuf<A>) -> 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 + PartialOrd,

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

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

fn partial_cmp(&self, other: &ItemBuf<A>) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

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

source§

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

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

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

source§

fn try_clone(&self) -> Result<ItemBuf<A>, Error>

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> TryFromIteratorIn<C, A> for ItemBuf<A>
where A: Allocator, C: IntoComponent,

source§

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

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

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

Auto Trait Implementations§

§

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> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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<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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

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

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
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>,

§

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>,

§

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,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

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