use super::*;
impl<E: Environment, I: IntegerType> Cast<Address<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<Address<E>> {
let field: Field<E> = self.cast()?;
field.cast()
}
}
impl<E: Environment, I: IntegerType> Cast<Boolean<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<Boolean<E>> {
if self.is_zero() {
Ok(Boolean::new(false))
} else if self.is_one() {
Ok(Boolean::new(true))
} else {
bail!("Failed to convert integer to boolean: integer is not zero or one")
}
}
}
impl<E: Environment, I: IntegerType> Cast<Field<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<Field<E>> {
self.to_field()
}
}
impl<E: Environment, I: IntegerType> Cast<Group<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<Group<E>> {
let field: Field<E> = self.cast()?;
field.cast()
}
}
impl<E: Environment, I0: IntegerType, I1: IntegerType + TryFrom<I0>> Cast<Integer<E, I1>> for Integer<E, I0> {
#[inline]
fn cast(&self) -> Result<Integer<E, I1>> {
Ok(Integer::<E, I1>::new(match I1::try_from(**self) {
Ok(value) => value,
Err(_) => bail!("Failed to convert '{}' into '{}'", I0::type_name(), I1::type_name()),
}))
}
}
impl<E: Environment, I: IntegerType> Cast<Scalar<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<Scalar<E>> {
let bits_le = self.to_bits_le();
Scalar::<E>::from_bits_le(&bits_le)
}
}
impl<E: Environment, I: IntegerType> Cast<IdentifierLiteral<E>> for Integer<E, I> {
#[inline]
fn cast(&self) -> Result<IdentifierLiteral<E>> {
let field: Field<E> = self.cast()?;
field.cast()
}
}