snarkvm_console_types_string/
lib.rs1#![cfg_attr(test, allow(clippy::assertions_on_result_states))]
17#![warn(clippy::cast_possible_truncation)]
18
19mod bitwise;
20mod bytes;
21mod parse;
22mod random;
23mod serialize;
24
25pub use snarkvm_console_network_environment::prelude::*;
26pub use snarkvm_console_types_boolean::Boolean;
27pub use snarkvm_console_types_field::Field;
28pub use snarkvm_console_types_integers::Integer;
29
30use core::marker::PhantomData;
31
32#[derive(Clone, PartialEq, Eq, Hash)]
33pub struct StringType<E: Environment> {
34 string: String,
36 _phantom: PhantomData<E>,
38}
39
40impl<E: Environment> StringTrait for StringType<E> {}
41
42impl<E: Environment> StringType<E> {
43 pub fn new(string: &str) -> Self {
45 let num_bytes = string.len();
47 match num_bytes <= E::MAX_STRING_BYTES as usize {
48 true => Self { string: string.to_string(), _phantom: PhantomData },
49 false => E::halt(format!("Attempted to allocate a string of size {num_bytes}")),
50 }
51 }
52}
53
54impl<E: Environment> TypeName for StringType<E> {
55 #[inline]
57 fn type_name() -> &'static str {
58 "string"
59 }
60}
61
62impl<E: Environment> Deref for StringType<E> {
63 type Target = str;
64
65 #[inline]
66 fn deref(&self) -> &Self::Target {
67 self.string.as_str()
68 }
69}