snarkvm_console_program/data/literal/cast/
mod.rs1mod boolean;
17mod field;
18mod integer;
19mod scalar;
20
21use crate::{Literal, LiteralType};
22use snarkvm_console_network::Network;
23use snarkvm_console_types::{Boolean, integers::Integer, prelude::*};
24
25pub trait Cast<T: Sized = Self> {
27 fn cast(&self) -> Result<T>;
32}
33
34impl<N: Network> Literal<N> {
35 pub fn cast(&self, to_type: LiteralType) -> Result<Self> {
47 match self {
48 Self::Address(address) => cast_group_to_type(address.to_group(), to_type),
49 Self::Boolean(boolean) => cast_boolean_to_type(boolean, to_type),
50 Self::Field(field) => cast_field_to_type(field, to_type),
51 Self::Group(group) => cast_group_to_type(group, to_type),
52 Self::I8(integer) => cast_integer_to_type(integer, to_type),
53 Self::I16(integer) => cast_integer_to_type(integer, to_type),
54 Self::I32(integer) => cast_integer_to_type(integer, to_type),
55 Self::I64(integer) => cast_integer_to_type(integer, to_type),
56 Self::I128(integer) => cast_integer_to_type(integer, to_type),
57 Self::U8(integer) => cast_integer_to_type(integer, to_type),
58 Self::U16(integer) => cast_integer_to_type(integer, to_type),
59 Self::U32(integer) => cast_integer_to_type(integer, to_type),
60 Self::U64(integer) => cast_integer_to_type(integer, to_type),
61 Self::U128(integer) => cast_integer_to_type(integer, to_type),
62 Self::Scalar(scalar) => cast_scalar_to_type(scalar, to_type),
63 Self::Signature(..) => bail!("Cannot cast a signature literal to another type."),
64 Self::String(..) => bail!("Cannot cast a string literal to another type."),
65 }
66 }
67}
68
69macro_rules! impl_cast_body {
71 ($type_name:ident, $cast:ident, $input:expr, $to_type:expr) => {
72 match $to_type {
73 LiteralType::Address => Ok(Literal::Address($input.$cast()?)),
74 LiteralType::Boolean => Ok(Literal::Boolean($input.$cast()?)),
75 LiteralType::Field => Ok(Literal::Field($input.$cast()?)),
76 LiteralType::Group => Ok(Literal::Group($input.$cast()?)),
77 LiteralType::I8 => Ok(Literal::I8($input.$cast()?)),
78 LiteralType::I16 => Ok(Literal::I16($input.$cast()?)),
79 LiteralType::I32 => Ok(Literal::I32($input.$cast()?)),
80 LiteralType::I64 => Ok(Literal::I64($input.$cast()?)),
81 LiteralType::I128 => Ok(Literal::I128($input.$cast()?)),
82 LiteralType::U8 => Ok(Literal::U8($input.$cast()?)),
83 LiteralType::U16 => Ok(Literal::U16($input.$cast()?)),
84 LiteralType::U32 => Ok(Literal::U32($input.$cast()?)),
85 LiteralType::U64 => Ok(Literal::U64($input.$cast()?)),
86 LiteralType::U128 => Ok(Literal::U128($input.$cast()?)),
87 LiteralType::Scalar => Ok(Literal::Scalar($input.$cast()?)),
88 LiteralType::Signature => {
89 bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a signature type."))
90 }
91 LiteralType::String => {
92 bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a string type."))
93 }
94 }
95 };
96}
97
98fn cast_boolean_to_type<N: Network>(input: &Boolean<N>, to_type: LiteralType) -> Result<Literal<N>> {
100 impl_cast_body!(boolean, cast, input, to_type)
101}
102
103fn cast_field_to_type<N: Network>(input: &Field<N>, to_type: LiteralType) -> Result<Literal<N>> {
105 impl_cast_body!(field, cast, input, to_type)
106}
107
108fn cast_group_to_type<N: Network>(input: &Group<N>, to_type: LiteralType) -> Result<Literal<N>> {
110 match to_type {
111 LiteralType::Address => Ok(Literal::Address(Address::new(*input))),
112 LiteralType::Group => Ok(Literal::Group(*input)),
113 _ => cast_field_to_type(&input.to_x_coordinate(), to_type),
114 }
115}
116
117fn cast_integer_to_type<N: Network, I: IntegerType>(
119 input: &integers::Integer<N, I>,
120 to_type: LiteralType,
121) -> Result<Literal<N>>
122where
123 i8: TryFrom<I>,
124 i16: TryFrom<I>,
125 i32: TryFrom<I>,
126 i64: TryFrom<I>,
127 i128: TryFrom<I>,
128 u8: TryFrom<I>,
129 u16: TryFrom<I>,
130 u32: TryFrom<I>,
131 u64: TryFrom<I>,
132 u128: TryFrom<I>,
133{
134 impl_cast_body!(integer, cast, input, to_type)
135}
136
137fn cast_scalar_to_type<N: Network>(input: &Scalar<N>, to_type: LiteralType) -> Result<Literal<N>> {
139 impl_cast_body!(scalar, cast, input, to_type)
140}