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 thedata
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,
impl<A> ItemBuf<A>where
A: Allocator,
Sourcepub fn push<C>(&mut self, c: C) -> Result<(), Error>where
C: IntoComponent,
pub fn push<C>(&mut self, c: C) -> Result<(), Error>where
C: IntoComponent,
Push the given component to the current item.
Sourcepub fn pop(&mut self) -> bool
pub fn pop(&mut self) -> bool
Pop a the tail component, returning true
if there was something to pop.
Source§impl ItemBuf
impl ItemBuf
Sourcepub const fn new() -> ItemBuf
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);
Sourcepub fn with_item(
iter: impl IntoIterator- ,
) -> Result<ItemBuf, Error>
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);
Sourcepub fn with_crate(name: &str) -> Result<ItemBuf, Error>
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);
Sourcepub fn with_crate_item<I>(name: &str, iter: I) -> Result<ItemBuf, Error>
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>§
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_vec(&self) -> Result<Vec<Component>, Error>
pub fn as_vec(&self) -> Result<Vec<Component>, Error>
Construct a new vector from the current item.
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- ,
) -> Result<ItemBuf, Error>
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")));
Sourcepub fn extended<C>(&self, part: C) -> Result<ItemBuf, Error>where
C: IntoComponent,
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")));
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), Error>
pub fn ancestry<U>(&self, other: U) -> Result<(ItemBuf, ItemBuf), Error>
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<'de, A> Deserialize<'de> for ItemBuf<A>
impl<'de, A> Deserialize<'de> for ItemBuf<A>
Source§fn deserialize<D>(
deserializer: D,
) -> Result<ItemBuf<A>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<ItemBuf<A>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'a, A> IntoIterator for &'a ItemBuf<A>where
A: Allocator,
impl<'a, A> IntoIterator for &'a ItemBuf<A>where
A: Allocator,
Source§impl<A> Ord for ItemBuf<A>where
A: Allocator,
impl<A> Ord for ItemBuf<A>where
A: Allocator,
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<A> PartialOrd for ItemBuf<A>where
A: Allocator,
impl<A> PartialOrd for ItemBuf<A>where
A: Allocator,
Source§impl<A> Serialize for ItemBuf<A>where
A: Allocator,
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,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl<C, A> TryFromIteratorIn<C, A> for ItemBuf<A>where
A: Allocator,
C: IntoComponent,
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>,
fn try_from_iter_in<T>(iter: T, alloc: A) -> Result<ItemBuf<A>, Error>where
T: IntoIterator<Item = C>,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.