godot_binary_serialization/types/
primitive.rs1use std::hash::Hash;
2
3use super::{variant::{AsVariant, GodotVariant}, TYPE_PADDING};
4
5#[derive(Debug)]
7pub struct GodotNull;
8
9impl GodotVariant for GodotNull {
10 fn byte_length(&self) -> usize {
11 TYPE_PADDING as usize
12 }
13
14 fn as_any(&self) -> &dyn std::any::Any {
15 self
16 }
17
18 fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
19 other.as_var::<GodotNull>().is_some()
20 }
21
22 fn bytes(&self) -> Vec<u8> {
23 format!("{:?}", self).as_bytes().to_vec()
24 }
25}
26
27#[derive(Debug, Clone, Copy)]
29pub struct GodotInteger {
30 pub value: i64,
31 pub byte_size: usize,
32}
33
34impl GodotInteger {
35 const BIT_32_SIZE: usize = 4;
36 const BIT_64_SIZE: usize = 8;
37 pub fn new_from_i32(v: i32) -> Self {
38 Self {
39 value: v as i64,
40 byte_size: TYPE_PADDING as usize + Self::BIT_32_SIZE,
41 }
42 }
43
44 pub fn new_from_i64(v: i64) -> Self {
45 Self {
46 value: v,
47 byte_size: TYPE_PADDING as usize + Self::BIT_64_SIZE,
48 }
49 }
50}
51
52impl GodotVariant for GodotInteger {
53 fn byte_length(&self) -> usize {
54 self.byte_size
55 }
56 fn as_any(&self) -> &dyn std::any::Any {
57 self
58 }
59
60 fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
61 if let Some(other) = other.as_var::<GodotInteger>() {
62 self.value == other.value
63 } else {
64 false
65 }
66 }
67
68 fn bytes(&self) -> Vec<u8> {
69 self.value.to_string().as_bytes().to_vec()
70 }
71}
72
73#[derive(Debug, Clone, Copy)]
75pub struct GodotFloat {
76 pub value: f64,
77 pub byte_size: usize,
78}
79
80impl GodotFloat {
81 pub const BIT_32_SIZE: usize = 4;
82 pub const BIT_64_SIZE: usize = 8;
83 pub fn new_from_f32(v: f32) -> Self {
84 Self {
85 value: v as f64,
86 byte_size: TYPE_PADDING as usize + Self::BIT_32_SIZE,
87 }
88 }
89
90 pub fn new_from_f64(v: f64) -> Self {
91 Self {
92 value: v,
93 byte_size: TYPE_PADDING as usize + Self::BIT_64_SIZE,
94 }
95 }
96}
97
98impl GodotVariant for GodotFloat {
99 fn byte_length(&self) -> usize {
100 self.byte_size
101 }
102
103 fn as_any(&self) -> &dyn std::any::Any {
104 self
105 }
106
107 fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
108 if let Some(other) = other.as_var::<GodotFloat>() {
109 self.value == other.value
110 } else {
111 false
112 }
113 }
114
115 fn bytes(&self) -> Vec<u8> {
116 self.value.to_string().as_bytes().to_vec()
117 }
118}
119
120#[derive(Debug, Hash, PartialEq, Eq, Clone)]
122pub struct GodotString {
123 pub value: String,
124 pub byte_size: usize,
125}
126
127impl GodotString {
128 pub fn new(s: &str) -> Self {
129 let length = s.len();
130 let pad = (4 - (length % 4)) % 4;
132 Self {
133 value: s.to_owned(),
134 byte_size: TYPE_PADDING as usize + pad + length,
135 }
136 }
137}
138
139impl GodotVariant for GodotString {
140 fn byte_length(&self) -> usize {
141 self.byte_size
142 }
143
144 fn as_any(&self) -> &dyn std::any::Any {
145 self
146 }
147
148 fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
149 if let Some(other) = other.as_var::<GodotString>() {
150 self.value == other.value
151 } else {
152 false
153 }
154 }
155
156 fn bytes(&self) -> Vec<u8> {
157 self.value.as_bytes().to_vec()
158 }
159}
160
161#[derive(Debug, Clone, Copy)]
163pub struct GodotBool {
164 pub value: bool,
165}
166
167impl GodotBool {
168 pub const BIT_SIZE: usize = 4;
169
170 pub fn new(r#bool: bool) -> Self {
171 Self { value: r#bool }
172 }
173}
174
175impl GodotVariant for GodotBool {
176 fn byte_length(&self) -> usize {
177 TYPE_PADDING as usize + Self::BIT_SIZE
178 }
179
180 fn as_any(&self) -> &dyn std::any::Any {
181 self
182 }
183
184 fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
185 if let Some(other) = other.as_var::<GodotBool>() {
186 self.value == other.value
187 } else {
188 false
189 }
190 }
191
192 fn bytes(&self) -> Vec<u8> {
193 format!("{:?}", self.value).as_bytes().to_vec()
194 }
195}