Struct rune::compile::Item

source ·
pub struct Item { /* private fields */ }
Expand description

The reference to an ItemBuf.

Implementations§

source§

impl Item

source

pub const fn new() -> &'static Item

Construct an Item corresponding to the root item.

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

assert_eq!(Item::new(), &*ItemBuf::new());
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 AsRef<Item> for &Item

source§

fn as_ref(&self) -> &Item

Converts this type into a shared reference of the (usually inferred) input type.
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 Debug for Item

source§

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

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

impl Default for &Item

source§

fn default() -> &Item

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

impl Display for Item

Format implementation for an ItemBuf.

An empty item is formatted as {root}, because it refers to the topmost root module.

§Examples

use rune::compile::{ComponentRef, ItemBuf};
use rune::alloc::prelude::*;

let root = ItemBuf::new().try_to_string()?;
assert_eq!("{root}", root);

let hello = ItemBuf::with_item(&[ComponentRef::Str("hello"), ComponentRef::Id(0)])?;
assert_eq!("hello::$0", hello.try_to_string()?);
source§

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

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

impl Hash for Item

source§

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

Feeds this value into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a Item

§

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 Item as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl Ord for Item

source§

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

This method returns an Ordering between self and other. 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 PartialEq<Iter<'_>> for &Item

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 PartialEq<Iter<'_>> for Item

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 PartialEq for Item

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 PartialOrd for Item

source§

fn partial_cmp(&self, other: &Item) -> 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 Serialize for Item

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 TryToOwned for Item

§

type Owned = ItemBuf

The resulting type after obtaining ownership.
source§

fn try_to_owned(&self) -> Result<<Item as TryToOwned>::Owned, Error>

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

impl Eq for Item

source§

impl StructuralPartialEq for Item

Auto Trait Implementations§

§

impl RefUnwindSafe for Item

§

impl Send for Item

§

impl !Sized for Item

§

impl Sync for Item

§

impl Unpin for Item

§

impl UnwindSafe for Item

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more