chandeliers_std/
cast.rs

1//! Type conversion functions.
2
3use chandeliers_sem::traits::Step;
4use chandeliers_sem::ty;
5
6/// Lustre node that converts an `int` to a `float`.
7#[derive(Debug, Clone, Default)]
8pub struct float_of_int {}
9
10impl Step for float_of_int {
11    type Input = i64;
12    type Output = f64;
13    #[inline]
14    #[expect(clippy::cast_precision_loss, reason = "Required by the user")]
15    fn step(&mut self, inputs: ty!(i64)) -> ty!(f64) {
16        inputs.map(|i| i as f64)
17    }
18}
19
20/// Lustre node that converts a `float` to an `int`.
21#[derive(Debug, Clone, Default)]
22pub struct int_of_float {}
23
24impl Step for int_of_float {
25    type Input = f64;
26    type Output = i64;
27
28    #[inline]
29    #[expect(clippy::cast_possible_truncation, reason = "Required by the user")]
30    fn step(&mut self, inputs: ty!(f64)) -> ty!(i64) {
31        inputs.map(|i| i as i64)
32    }
33}
34
35/// Rounds a `float` to the int above.
36#[derive(Debug, Clone, Default)]
37pub struct ceil {}
38
39impl Step for ceil {
40    type Input = f64;
41    type Output = f64;
42
43    #[inline]
44    fn step(&mut self, inputs: ty!(f64)) -> ty!(f64) {
45        inputs.map(f64::ceil)
46    }
47}
48
49/// Rounds a `float` to the int below.
50#[derive(Debug, Clone, Default)]
51pub struct floor {}
52
53impl Step for floor {
54    type Input = f64;
55    type Output = f64;
56
57    #[inline]
58    fn step(&mut self, inputs: ty!(f64)) -> ty!(f64) {
59        inputs.map(f64::floor)
60    }
61}