diff --git a/src/main.rs b/src/main.rs index c01853eccc0423c96a6687929ee23f919e53e066..26f4e3b5dcadef34ef53f3b051ffd61ed76f1df9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,10 @@ fn main() { let mut out = [false, false]; let out = &Signal::new(&mut out); + // let s = &(&*out)[0..3]; // .to_slice(0, 1); + let out0: Signal<1> = out.view(0, 1); + let out1: Signal<1> = out.view(1, 2); + // println!("s {:b}", &s); let m = Mux { ins: (Signal::new(&mut in1), Signal::new(&mut in2)), @@ -111,6 +115,10 @@ fn main() { sel.store_slice(&[false]); m.eval(); println!("m.out {:b}", m.out); + println!("out0 {:b}", &out0); + println!("out1 {:b}", &out1); + let p = &m.out[0..1]; + let m: Signal<1> = m.out[0..2].into(); } // #[derive(Debug)] // struct Mux<'a, const I: usize, const S: usize, const N: usize> { diff --git a/src/signal.rs b/src/signal.rs index efa270940be50d34ccdf3d85b1685bfc94c673eb..b277860a7d219d725d7b3a40e928bd9be6758d0e 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -1,13 +1,12 @@ use std::cell::Cell; use std::convert::{From, Into}; use std::fmt; -use std::ops::{BitAnd, BitOr, Deref, Index}; +use std::ops::{BitAnd, BitOr, Deref, Index, Range}; use std::string::String; #[derive(Debug, PartialEq, Clone)] pub struct Signal<'a, const N: usize> { - // we should probably have set/get instead of exposing store pub store: &'a [Cell<bool>], } @@ -38,6 +37,13 @@ impl<'a, const N: usize> Signal<'a, N> { v.set(a[i]) } } + + pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> { + assert!(M == t - f); + Signal { + store: &self.store[f..t], + } + } } // impl<'a, const N: usize> From<u8> for Signal<'a, N> { @@ -50,6 +56,13 @@ impl<'a, const N: usize> Signal<'a, N> { // } // } +impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> { + fn from(slice: &'a [Cell<bool>]) -> Self { + assert!(N == slice.len()); + Signal { store: slice } + } +} + impl<'a, const N: usize> Deref for Signal<'a, N> { type Target = [Cell<bool>]; @@ -68,16 +81,27 @@ impl<'a, const N: usize> fmt::Binary for &Signal<'a, N> { } } -impl<'a, const N: usize> Index<usize> for Signal<'a, N> { - type Output = Signal<'a, 1>; +// almost works +// impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> { +// type Output = Signal<'a, N>; - #[inline] - fn index(&self, index: usize) -> Self::Output { - Signal { - store: &[self.store[index].clone()], - } - } -} +// fn index(&self, index: Range<usize>) -> &Signal<'a, N> { +// &Signal { +// store: &self.store[index.start..index.end], +// } +// } +// } + +// impl<'a, const N: usize> Index<usize> for Signal<'a, N> { +// type Output = Signal<'a, 1>; + +// #[inline] +// fn index(&self, index: usize) -> Self::Output { +// Signal { +// store: &[self.store[index].clone()], +// } +// } +// } // operations on signal // impl<const N: usize> BitAnd for &Signal<N> {