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_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> {
......
......@@ -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();
......
......@@ -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> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment