From 94d6e2af6c15226db680550932c2ae97b4f72dca Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Thu, 10 Jun 2021 14:17:36 +0200 Subject: [PATCH] with serde experiments --- src/bitarr.rs | 7 +-- src/main.rs | 5 +- src/signal.rs | 144 +++++++++++++++++++++++++------------------------- 3 files changed, 80 insertions(+), 76 deletions(-) diff --git a/src/bitarr.rs b/src/bitarr.rs index 963c528..94c3357 100644 --- a/src/bitarr.rs +++ b/src/bitarr.rs @@ -1,13 +1,14 @@ use serde::{Deserialize, Serialize}; + +use serde_json; use std::cell::Cell; use std::convert::TryFrom; use std::convert::{From, Into}; use std::fmt; use std::ops::{BitAnd, BitOr, Deref, DerefMut, Index, Range}; use std::string::String; - -#[derive(Debug, PartialEq, Clone)] -pub struct BitArr<const N: usize>(pub [bool; N]); +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)] +pub struct BitArr<const N: usize>(#[serde(with = "serde_arrays")] pub [bool; N]); // operations on BitArr impl<const N: usize> BitArr<N> { diff --git a/src/main.rs b/src/main.rs index 839f858..a1e26a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,10 +76,13 @@ struct MyArr<const N: usize> { arr: [bool; N], } +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct BA<const N: usize>(#[serde(with = "serde_arrays")] [bool; N]); + #[test] fn test_serde() { println!("test serde"); - let arr = MyArr { arr: [false; 8] }; + let arr = BA([false; 8]); let json = serde_json::to_string(&arr).unwrap(); println!("json {:?}", json); let arr2 = serde_json::from_str(&json).unwrap(); diff --git a/src/signal.rs b/src/signal.rs index 430cacd..03561db 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -14,83 +14,83 @@ pub struct Signal<'a, const N: usize> { store: &'a [Cell<bool>; N], } -impl<'a, const N: usize> Signal<'a, N> { - // construct a signal from a mutable array - pub fn new(arr: &'a mut [bool; N]) -> Self { - let cell_slice: &'a Cell<[bool]> = Cell::from_mut(arr); - let slice_cell = cell_slice.as_slice_of_cells(); - let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); - Self { store: a } - } - - // read a signal value to a bit array - pub fn r(&self) -> BitArr<N> { - let mut a = [false; N]; - for (i, v) in self.store.iter().enumerate() { - a[i] = v.get() - } - BitArr(a) - } - - // write a signal to a bit array - pub fn w(&self, a: BitArr<N>) { - for (i, v) in self.store.iter().enumerate() { - v.set(a[i]) - } - } - - // not sure if we should support this - pub fn store_slice(&self, a: &[bool]) { - for (i, v) in self.store.iter().enumerate() { - v.set(a[i]) - } - } - - // create a view of the signal - // for now this view provides both reading and writing - // not sure if this is what we want in the end - pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> { - let slice_cell = &self.store[f..t]; - let a = <&[Cell<bool>; M]>::try_from(slice_cell).unwrap(); - Signal { store: a } - } -} +// impl<'a, const N: usize> Signal<'a, N> { +// // construct a signal from a mutable array +// pub fn new(arr: &'a mut [bool; N]) -> Self { +// let cell_slice: &'a Cell<[bool]> = Cell::from_mut(arr); +// let slice_cell = cell_slice.as_slice_of_cells(); +// let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); +// Self { store: a } +// } -// create a signal from a bit array -impl<'a, const N: usize> From<&'a mut BitArr<N>> for Signal<'a, N> { - fn from(a: &'a mut BitArr<N>) -> Self { - Signal::new(&mut a.0) - } -} +// // read a signal value to a bit array +// pub fn r(&self) -> BitArr<N> { +// let mut a = [false; N]; +// for (i, v) in self.store.iter().enumerate() { +// a[i] = v.get() +// } +// BitArr(a) +// } -// create a signal from a slice of the bit array storage -impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> { - fn from(slice_cell: &'a [Cell<bool>]) -> Self { - let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); - Signal { store: a } - } -} +// // write a signal to a bit array +// pub fn w(&self, a: BitArr<N>) { +// for (i, v) in self.store.iter().enumerate() { +// v.set(a[i]) +// } +// } -// automatic dereferencing -impl<'a, const N: usize> Deref for Signal<'a, N> { - type Target = [Cell<bool>]; +// // not sure if we should support this +// pub fn store_slice(&self, a: &[bool]) { +// for (i, v) in self.store.iter().enumerate() { +// v.set(a[i]) +// } +// } - fn deref(&self) -> &Self::Target { - self.store - } -} +// // create a view of the signal +// // for now this view provides both reading and writing +// // not sure if this is what we want in the end +// pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> { +// let slice_cell = &self.store[f..t]; +// let a = <&[Cell<bool>; M]>::try_from(slice_cell).unwrap(); +// Signal { store: a } +// } +// } -// prints the value of a signal -// perhaps we should enforce reading the signal into a bit array first -impl<'a, const N: usize> fmt::Binary for Signal<'a, N> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut s = String::new(); - for i in self.store.iter().rev() { - s.push(if i.get() { '1' } else { '0' }); - } - write!(f, "{}", s) - } -} +// // create a signal from a bit array +// impl<'a, const N: usize> From<&'a mut BitArr<N>> for Signal<'a, N> { +// fn from(a: &'a mut BitArr<N>) -> Self { +// Signal::new(&mut a.0) +// } +// } + +// // create a signal from a slice of the bit array storage +// impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> { +// fn from(slice_cell: &'a [Cell<bool>]) -> Self { +// let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); +// Signal { store: a } +// } +// } + +// // automatic dereferencing +// impl<'a, const N: usize> Deref for Signal<'a, N> { +// type Target = [Cell<bool>]; + +// fn deref(&self) -> &Self::Target { +// self.store +// } +// } + +// // prints the value of a signal +// // perhaps we should enforce reading the signal into a bit array first +// impl<'a, const N: usize> fmt::Binary for Signal<'a, N> { +// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +// let mut s = String::new(); +// for i in self.store.iter().rev() { +// s.push(if i.get() { '1' } else { '0' }); +// } +// write!(f, "{}", s) +// } +// } // almost works // impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> { -- GitLab