logic_mesh/blocks/bitwise/
bitwise_not.rs

1// Copyright (c) 2022-2024, Radu Racariu.
2
3use libhaystack::val::Value;
4use uuid::Uuid;
5
6use crate::base::input::input_reader::InputReader;
7use crate::base::{
8    block::{Block, BlockDesc, BlockProps, BlockState},
9    input::{Input, InputProps},
10    output::Output,
11};
12
13use libhaystack::val::kind::HaystackKind;
14
15use crate::{blocks::InputImpl, blocks::OutputImpl};
16
17/// Outputs bitwise NOT operation.
18#[block]
19#[derive(BlockProps, Debug)]
20#[category = "bitwise"]
21pub struct BitwiseNot {
22    #[input(name = "in", kind = "Number")]
23    pub input: InputImpl,
24    #[output(kind = "Bool")]
25    pub out: OutputImpl,
26}
27
28impl Block for BitwiseNot {
29    async fn execute(&mut self) {
30        self.read_inputs_until_ready().await;
31
32        if let Some(Value::Number(n)) = self.input.get_value() {
33            self.out.value = Value::make_int(!(n.value as i64));
34        }
35    }
36}
37
38#[cfg(test)]
39mod test {
40    use crate::{
41        base::block::test_utils::write_block_inputs, base::block::Block,
42        blocks::bitwise::BitwiseNot,
43    };
44
45    #[tokio::test]
46    async fn test_and_op() {
47        let mut block = BitwiseNot::new();
48
49        write_block_inputs(&mut [(&mut block.input, 2.into())]).await;
50        block.execute().await;
51
52        assert_eq!(block.out.value, (-3).into());
53    }
54}