rune/runtime/value/
data.rs1use core::borrow::Borrow;
2use core::fmt;
3use core::hash;
4
5use crate::alloc::prelude::*;
6use crate::runtime::{BorrowRef, TypeInfo};
7use crate::sync::Arc;
8
9use super::{Rtti, Value};
10
11pub struct EmptyStruct<'a> {
13 pub(crate) rtti: &'a Arc<Rtti>,
15}
16
17impl<'a> EmptyStruct<'a> {
18 pub fn rtti(&self) -> &'a Arc<Rtti> {
20 self.rtti
21 }
22
23 pub fn type_info(&self) -> TypeInfo {
25 TypeInfo::rtti(self.rtti.clone())
26 }
27}
28
29impl fmt::Debug for EmptyStruct<'_> {
30 #[inline]
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 write!(f, "{}", self.rtti.item)
33 }
34}
35
36pub struct TupleStruct<'a> {
38 pub(crate) rtti: &'a Arc<Rtti>,
40 pub(crate) data: BorrowRef<'a, [Value]>,
42}
43
44impl<'a> TupleStruct<'a> {
45 pub fn rtti(&self) -> &'a Rtti {
47 self.rtti
48 }
49
50 pub fn data(&self) -> &[Value] {
52 &self.data
53 }
54
55 pub fn get(&self, index: usize) -> Option<&Value> {
57 self.data.get(index)
58 }
59
60 pub fn type_info(&self) -> TypeInfo {
62 TypeInfo::rtti(self.rtti.clone())
63 }
64}
65
66impl fmt::Debug for TupleStruct<'_> {
67 #[inline]
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(f, "{}", self.rtti.item)
70 }
71}
72
73pub struct Struct<'a> {
75 pub(crate) rtti: &'a Arc<Rtti>,
77 pub(crate) data: BorrowRef<'a, [Value]>,
79}
80
81impl<'a> Struct<'a> {
82 pub fn rtti(&self) -> &'a Arc<Rtti> {
84 self.rtti
85 }
86
87 pub fn data(&self) -> &[Value] {
89 &self.data
90 }
91
92 pub fn get<Q>(&self, key: &Q) -> Option<&Value>
94 where
95 Box<str>: Borrow<Q>,
96 Q: hash::Hash + Eq + ?Sized,
97 {
98 self.data.get(*self.rtti.fields.get(key)?)
99 }
100
101 pub(crate) fn type_info(&self) -> TypeInfo {
103 TypeInfo::rtti(self.rtti.clone())
104 }
105}
106
107impl fmt::Debug for Struct<'_> {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 write!(f, "{}", self.rtti.item)
110 }
111}