diff --git a/src/lib.rs b/src/lib.rs index 458c55a2ab7b799d9b808618a695a5f8f875b1bc..5906b1994203cce61b51bbf4341f0e60e606bada 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,125 +1,2 @@ -use std::convert::{From, Into}; -use std::fmt; -use std::ops::{BitAnd, Index}; -use std::string::String; - -#[derive(Debug, PartialEq, Clone)] -pub struct Signal(Vec<bool>); - -impl Signal { - pub fn new() -> Self { - Self { 0: Vec::new() } - } - - pub fn new_zeroed(n: usize) -> Self { - let mut bs = Signal::new(); - for i in 0..n { - bs.push(false) - } - bs - } - - pub fn push(&mut self, b: bool) { - self.0.push(b) - } - - pub fn pop(&mut self) -> Option<bool> { - self.0.pop() - } - - pub fn set(&mut self, i: usize, b: bool) { - self.0[i] = b; - } -} - -impl fmt::Binary for Signal { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut s = String::new(); - for i in self.0.iter().rev() { - s.push(if *i { '1' } else { '0' }); - } - write!(f, "{}", s) - } -} - -impl Index<usize> for Signal { - type Output = bool; - - #[inline] - fn index(&self, index: usize) -> &Self::Output { - &self.0[index] - } -} - -// operations on signal -impl BitAnd for &Signal { - type Output = Signal; - - // rhs is the "right-hand side" of the expression `a & b` - fn bitand(self, rhs: Self) -> Self::Output { - let mut bs = Signal::new(); - for (a, b) in self.0.iter().zip(&rhs.0) { - bs.push(a & b); - } - bs - } -} - -impl From<u8> for Signal { - fn from(u: u8) -> Self { - let mut bs = Signal::new(); - for i in 0..8 { - bs.push(u & (1 << i) != 0) - } - bs - } -} - -impl From<Signal> for u8 { - fn from(b: Signal) -> u8 { - let mut u = 0; - for (n, b) in b.0.iter().enumerate() { - u |= if *b { 1 << n } else { 0 } - } - u - } -} - -impl From<u32> for Signal { - fn from(u: u32) -> Self { - let mut bs = Signal::new(); - for i in 0..32 { - bs.push(u & (1 << i) != 0) - } - bs - } -} - -impl From<Signal> for u32 { - fn from(b: Signal) -> u32 { - let mut u = 0; - for (n, b) in b.0.iter().enumerate() { - u |= if *b { 1 << n } else { 0 } - } - u - } -} - -#[test] -fn test_partial_eq() { - let b: Signal = 12345u32.into(); - let c: Signal = 1245u32.into(); - assert!(b != c); - assert!(b == b); -} - -#[test] -fn test_and() { - let a = 0x15u8; - let b = 0x51u8; - - let a_bs: Signal = a.into(); - let b_bs: Signal = b.into(); - let c_bs = &a_bs & &b_bs; - assert!(a & b == c_bs.into()); -} +mod signal; +pub use signal::*; diff --git a/src/signal.rs b/src/signal.rs new file mode 100644 index 0000000000000000000000000000000000000000..458c55a2ab7b799d9b808618a695a5f8f875b1bc --- /dev/null +++ b/src/signal.rs @@ -0,0 +1,125 @@ +use std::convert::{From, Into}; +use std::fmt; +use std::ops::{BitAnd, Index}; +use std::string::String; + +#[derive(Debug, PartialEq, Clone)] +pub struct Signal(Vec<bool>); + +impl Signal { + pub fn new() -> Self { + Self { 0: Vec::new() } + } + + pub fn new_zeroed(n: usize) -> Self { + let mut bs = Signal::new(); + for i in 0..n { + bs.push(false) + } + bs + } + + pub fn push(&mut self, b: bool) { + self.0.push(b) + } + + pub fn pop(&mut self) -> Option<bool> { + self.0.pop() + } + + pub fn set(&mut self, i: usize, b: bool) { + self.0[i] = b; + } +} + +impl fmt::Binary for Signal { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut s = String::new(); + for i in self.0.iter().rev() { + s.push(if *i { '1' } else { '0' }); + } + write!(f, "{}", s) + } +} + +impl Index<usize> for Signal { + type Output = bool; + + #[inline] + fn index(&self, index: usize) -> &Self::Output { + &self.0[index] + } +} + +// operations on signal +impl BitAnd for &Signal { + type Output = Signal; + + // rhs is the "right-hand side" of the expression `a & b` + fn bitand(self, rhs: Self) -> Self::Output { + let mut bs = Signal::new(); + for (a, b) in self.0.iter().zip(&rhs.0) { + bs.push(a & b); + } + bs + } +} + +impl From<u8> for Signal { + fn from(u: u8) -> Self { + let mut bs = Signal::new(); + for i in 0..8 { + bs.push(u & (1 << i) != 0) + } + bs + } +} + +impl From<Signal> for u8 { + fn from(b: Signal) -> u8 { + let mut u = 0; + for (n, b) in b.0.iter().enumerate() { + u |= if *b { 1 << n } else { 0 } + } + u + } +} + +impl From<u32> for Signal { + fn from(u: u32) -> Self { + let mut bs = Signal::new(); + for i in 0..32 { + bs.push(u & (1 << i) != 0) + } + bs + } +} + +impl From<Signal> for u32 { + fn from(b: Signal) -> u32 { + let mut u = 0; + for (n, b) in b.0.iter().enumerate() { + u |= if *b { 1 << n } else { 0 } + } + u + } +} + +#[test] +fn test_partial_eq() { + let b: Signal = 12345u32.into(); + let c: Signal = 1245u32.into(); + assert!(b != c); + assert!(b == b); +} + +#[test] +fn test_and() { + let a = 0x15u8; + let b = 0x51u8; + + let a_bs: Signal = a.into(); + let b_bs: Signal = b.into(); + let c_bs = &a_bs & &b_bs; + assert!(a & b == c_bs.into()); +}