Skip to content

Commit

Permalink
wires: half an index impl
Browse files Browse the repository at this point in the history
  • Loading branch information
rrbutani committed Sep 28, 2019
1 parent ec874cf commit 5c986c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hdl/src/wires/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::{BitCountType, Wire};

use core::ops::{Index, IndexMut};

// Warning! Indexes by byte (not bit). This isn't very useful.
impl<const B: BitCountType, const S: usize> Index<usize> for Wire<{B}, {S}> {
type Output = u8;

fn index(&self, idx: usize) -> &u8 {
&self.repr[idx]
}
}

impl<const B: BitCountType, const S: usize> IndexMut<usize> for Wire<{B}, {S}> {
fn index_mut(&mut self, idx: usize) -> &mut u8 {
&mut self.repr[idx]
}
}


#[cfg(test)]
mod tests {
use super::*;
use crate::w;

#[test]
fn index_bytes() {
assert_eq!(w!(1#0)[0], 0u8);
assert_eq!(w!(8*8)[7], 0u8);
assert_eq!(w!((8*7)#core::u64::MAX)[7], 255u8);
}
}
1 change: 1 addition & 0 deletions hdl/src/wires/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
mod conversions;
mod fmt;
mod index;

use core::mem::{size_of, MaybeUninit};

Expand Down

0 comments on commit 5c986c8

Please sign in to comment.