Skip to content
Snippets Groups Projects
Commit 94d6e2af authored by Per Lindgren's avatar Per Lindgren
Browse files

with serde experiments

parent ffe92ce3
No related branches found
No related tags found
No related merge requests found
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json;
use std::cell::Cell; use std::cell::Cell;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::convert::{From, Into}; use std::convert::{From, Into};
use std::fmt; use std::fmt;
use std::ops::{BitAnd, BitOr, Deref, DerefMut, Index, Range}; use std::ops::{BitAnd, BitOr, Deref, DerefMut, Index, Range};
use std::string::String; use std::string::String;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
#[derive(Debug, PartialEq, Clone)] pub struct BitArr<const N: usize>(#[serde(with = "serde_arrays")] pub [bool; N]);
pub struct BitArr<const N: usize>(pub [bool; N]);
// operations on BitArr // operations on BitArr
impl<const N: usize> BitArr<N> { impl<const N: usize> BitArr<N> {
......
...@@ -76,10 +76,13 @@ struct MyArr<const N: usize> { ...@@ -76,10 +76,13 @@ struct MyArr<const N: usize> {
arr: [bool; N], arr: [bool; N],
} }
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct BA<const N: usize>(#[serde(with = "serde_arrays")] [bool; N]);
#[test] #[test]
fn test_serde() { fn test_serde() {
println!("test serde"); println!("test serde");
let arr = MyArr { arr: [false; 8] }; let arr = BA([false; 8]);
let json = serde_json::to_string(&arr).unwrap(); let json = serde_json::to_string(&arr).unwrap();
println!("json {:?}", json); println!("json {:?}", json);
let arr2 = serde_json::from_str(&json).unwrap(); let arr2 = serde_json::from_str(&json).unwrap();
......
...@@ -14,83 +14,83 @@ pub struct Signal<'a, const N: usize> { ...@@ -14,83 +14,83 @@ pub struct Signal<'a, const N: usize> {
store: &'a [Cell<bool>; N], store: &'a [Cell<bool>; N],
} }
impl<'a, const N: usize> Signal<'a, N> { // impl<'a, const N: usize> Signal<'a, N> {
// construct a signal from a mutable array // // construct a signal from a mutable array
pub fn new(arr: &'a mut [bool; N]) -> Self { // pub fn new(arr: &'a mut [bool; N]) -> Self {
let cell_slice: &'a Cell<[bool]> = Cell::from_mut(arr); // let cell_slice: &'a Cell<[bool]> = Cell::from_mut(arr);
let slice_cell = cell_slice.as_slice_of_cells(); // let slice_cell = cell_slice.as_slice_of_cells();
let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); // let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap();
Self { store: a } // Self { store: a }
} // }
// read a signal value to a bit array // // read a signal value to a bit array
pub fn r(&self) -> BitArr<N> { // pub fn r(&self) -> BitArr<N> {
let mut a = [false; N]; // let mut a = [false; N];
for (i, v) in self.store.iter().enumerate() { // for (i, v) in self.store.iter().enumerate() {
a[i] = v.get() // a[i] = v.get()
} // }
BitArr(a) // BitArr(a)
} // }
// write a signal to a bit array // // write a signal to a bit array
pub fn w(&self, a: BitArr<N>) { // pub fn w(&self, a: BitArr<N>) {
for (i, v) in self.store.iter().enumerate() { // for (i, v) in self.store.iter().enumerate() {
v.set(a[i]) // v.set(a[i])
} // }
} // }
// not sure if we should support this // // not sure if we should support this
pub fn store_slice(&self, a: &[bool]) { // pub fn store_slice(&self, a: &[bool]) {
for (i, v) in self.store.iter().enumerate() { // for (i, v) in self.store.iter().enumerate() {
v.set(a[i]) // v.set(a[i])
} // }
} // }
// create a view of the signal // // create a view of the signal
// for now this view provides both reading and writing // // for now this view provides both reading and writing
// not sure if this is what we want in the end // // not sure if this is what we want in the end
pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> { // pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> {
let slice_cell = &self.store[f..t]; // let slice_cell = &self.store[f..t];
let a = <&[Cell<bool>; M]>::try_from(slice_cell).unwrap(); // let a = <&[Cell<bool>; M]>::try_from(slice_cell).unwrap();
Signal { store: a } // Signal { store: a }
} // }
} // }
// create a signal from a bit array // // create a signal from a bit array
impl<'a, const N: usize> From<&'a mut BitArr<N>> for Signal<'a, N> { // impl<'a, const N: usize> From<&'a mut BitArr<N>> for Signal<'a, N> {
fn from(a: &'a mut BitArr<N>) -> Self { // fn from(a: &'a mut BitArr<N>) -> Self {
Signal::new(&mut a.0) // Signal::new(&mut a.0)
} // }
} // }
// create a signal from a slice of the bit array storage // // create a signal from a slice of the bit array storage
impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> { // impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> {
fn from(slice_cell: &'a [Cell<bool>]) -> Self { // fn from(slice_cell: &'a [Cell<bool>]) -> Self {
let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap(); // let a = <&[Cell<bool>; N]>::try_from(slice_cell).unwrap();
Signal { store: a } // Signal { store: a }
} // }
} // }
// automatic dereferencing // // automatic dereferencing
impl<'a, const N: usize> Deref for Signal<'a, N> { // impl<'a, const N: usize> Deref for Signal<'a, N> {
type Target = [Cell<bool>]; // type Target = [Cell<bool>];
fn deref(&self) -> &Self::Target { // fn deref(&self) -> &Self::Target {
self.store // self.store
} // }
} // }
// prints the value of a signal // // prints the value of a signal
// perhaps we should enforce reading the signal into a bit array first // // perhaps we should enforce reading the signal into a bit array first
impl<'a, const N: usize> fmt::Binary for Signal<'a, N> { // impl<'a, const N: usize> fmt::Binary for Signal<'a, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new(); // let mut s = String::new();
for i in self.store.iter().rev() { // for i in self.store.iter().rev() {
s.push(if i.get() { '1' } else { '0' }); // s.push(if i.get() { '1' } else { '0' });
} // }
write!(f, "{}", s) // write!(f, "{}", s)
} // }
} // }
// almost works // almost works
// impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> { // impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment