rune

Struct 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) -> bool

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

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::ItemBuf;

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

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

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

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

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

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

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

Source§

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,

Source§

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,

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) -> <&'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,

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

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> PartialEq<Item> for &ItemBuf<A>
where A: Allocator,

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> PartialEq<Item> for ItemBuf<A>
where A: Allocator,

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<A> PartialEq<Iter<'_>> for &ItemBuf<A>
where A: Allocator,

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> PartialEq<Iter<'_>> for ItemBuf<A>
where A: Allocator,

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> PartialEq for ItemBuf<A>
where A: Allocator,

Source§

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

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> 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> 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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T