/ console / program / src / data / literal / cast / boolean.rs
boolean.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  use super::*;
17  use crate::data::literal::cast_lossy::CastLossy;
18  
19  impl<E: Environment> Cast<Address<E>> for Boolean<E> {
20      /// Casts a `Boolean` to an `Address`.
21      #[inline]
22      fn cast(&self) -> Result<Address<E>> {
23          Ok(self.cast_lossy())
24      }
25  }
26  
27  impl<E: Environment> Cast<Boolean<E>> for Boolean<E> {
28      /// Casts a `Boolean` to a `Boolean`.
29      #[inline]
30      fn cast(&self) -> Result<Boolean<E>> {
31          Ok(self.cast_lossy())
32      }
33  }
34  
35  impl<E: Environment> Cast<Field<E>> for Boolean<E> {
36      /// Casts a `Boolean` to a `Field`.
37      #[inline]
38      fn cast(&self) -> Result<Field<E>> {
39          Ok(self.cast_lossy())
40      }
41  }
42  
43  impl<E: Environment> Cast<Group<E>> for Boolean<E> {
44      /// Casts a `Boolean` to a `Group`.
45      #[inline]
46      fn cast(&self) -> Result<Group<E>> {
47          Ok(self.cast_lossy())
48      }
49  }
50  
51  impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Boolean<E> {
52      /// Casts a `Boolean` to an `Integer`.
53      #[inline]
54      fn cast(&self) -> Result<Integer<E, I>> {
55          match **self {
56              true => Ok(Integer::one()),
57              false => Ok(Integer::zero()),
58          }
59      }
60  }
61  
62  impl<E: Environment> Cast<Scalar<E>> for Boolean<E> {
63      /// Casts a `Boolean` to a `Scalar`.
64      #[inline]
65      fn cast(&self) -> Result<Scalar<E>> {
66          Ok(self.cast_lossy())
67      }
68  }