rune/runtime/value/
data.rsuse core::borrow::Borrow;
use core::fmt;
use core::hash;
use rust_alloc::sync::Arc;
use crate::alloc::prelude::*;
use crate::runtime::{BorrowRef, TypeInfo};
use super::{Rtti, Value};
pub struct EmptyStruct<'a> {
pub(crate) rtti: &'a Arc<Rtti>,
}
impl<'a> EmptyStruct<'a> {
pub fn rtti(&self) -> &'a Arc<Rtti> {
self.rtti
}
pub fn type_info(&self) -> TypeInfo {
TypeInfo::rtti(self.rtti.clone())
}
}
impl fmt::Debug for EmptyStruct<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.rtti.item)
}
}
pub struct TupleStruct<'a> {
pub(crate) rtti: &'a Arc<Rtti>,
pub(crate) data: BorrowRef<'a, [Value]>,
}
impl<'a> TupleStruct<'a> {
pub fn rtti(&self) -> &'a Rtti {
self.rtti
}
pub fn data(&self) -> &[Value] {
&self.data
}
pub fn get(&self, index: usize) -> Option<&Value> {
self.data.get(index)
}
pub fn type_info(&self) -> TypeInfo {
TypeInfo::rtti(self.rtti.clone())
}
}
impl fmt::Debug for TupleStruct<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.rtti.item)
}
}
pub struct Struct<'a> {
pub(crate) rtti: &'a Arc<Rtti>,
pub(crate) data: BorrowRef<'a, [Value]>,
}
impl<'a> Struct<'a> {
pub fn rtti(&self) -> &'a Arc<Rtti> {
self.rtti
}
pub fn data(&self) -> &[Value] {
&self.data
}
pub fn get<Q>(&self, key: &Q) -> Option<&Value>
where
Box<str>: Borrow<Q>,
Q: hash::Hash + Eq + ?Sized,
{
self.data.get(*self.rtti.fields.get(key)?)
}
pub(crate) fn type_info(&self) -> TypeInfo {
TypeInfo::rtti(self.rtti.clone())
}
}
impl fmt::Debug for Struct<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.rtti.item)
}
}