pub struct Item { /* private fields */ }
Expand description
The reference to an ItemBuf.
Implementations§
Source§impl Item
impl Item
Sourcepub const unsafe fn from_bytes(content: &[u8]) -> &Self
pub const unsafe fn from_bytes(content: &[u8]) -> &Self
Construct an Item from an ItemBuf.
§Safety
Caller must ensure that content has a valid ItemBuf representation.
The easiest way to accomplish this is to use the rune::item!
macro.
§Examples
use rune::{Item, ItemBuf};
let item = ItemBuf::with_item(["foo", "bar"])?;
// SAFETY: item is constructed from a valid buffer.
let item = unsafe { Item::from_bytes(item.as_bytes()) };
Sourcepub fn as_crate(&self) -> Option<&str>
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);
Sourcepub fn first(&self) -> Option<ComponentRef<'_>>
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")));
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn as_local(&self) -> Option<&str>
pub fn as_local(&self) -> Option<&str>
If the item only contains one element, return that element.
Sourcepub fn join(
&self,
other: impl IntoIterator<Item: IntoComponent>,
) -> Result<ItemBuf>
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")));
Sourcepub fn extended<C>(&self, part: C) -> Result<ItemBuf>where
C: IntoComponent,
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")));
Sourcepub fn last(&self) -> Option<ComponentRef<'_>>
pub fn last(&self) -> Option<ComponentRef<'_>>
Access the last component in the path.
Sourcepub fn base_name(&self) -> Option<&str>
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.
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
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());
Sourcepub fn starts_with<U>(&self, other: U) -> bool
pub fn starts_with<U>(&self, other: U) -> bool
Test if current item starts with another.
Sourcepub fn is_super_of<U>(&self, other: U, n: usize) -> bool
pub fn is_super_of<U>(&self, other: U, n: usize) -> bool
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));
Sourcepub fn ancestry<U>(&self, other: U) -> Result<(ItemBuf, ItemBuf)>
pub fn ancestry<U>(&self, other: U) -> Result<(ItemBuf, ItemBuf)>
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"])?)?
);
Sourcepub fn parent(&self) -> Option<&Item>
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));
Sourcepub fn unqalified(&self) -> Unqalified<'_>
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 Display for Item
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::alloc::prelude::*;
use rune::ItemBuf;
use rune::item::ComponentRef;
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()?);