Skip to content

Commit 25921f1

Browse files
committed
added some helpful macros to implement indexable items, but do not mandate it for slice wrappers. Also add len() function
1 parent cfd11db commit 25921f1

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

src/allocated_memory/index_macro.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#[macro_export]
2+
macro_rules! define_index_ops_mut {
3+
($T:ident, $MemoryType:ty) => {
4+
impl<$T> ::core::ops::Index<usize> for $MemoryType
5+
{
6+
type Output = T;
7+
8+
#[inline]
9+
fn index(&self, index: usize) -> &Self::Output {
10+
::core::ops::Index::index(&**self, index)
11+
}
12+
}
13+
14+
impl<$T> ::core::ops::IndexMut<usize> for $MemoryType
15+
{
16+
#[inline]
17+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
18+
::core::ops::IndexMut::index_mut(&mut **self, index)
19+
}
20+
}
21+
22+
23+
impl<$T> ::core::ops::Index<::core::ops::Range<usize>> for $MemoryType
24+
{
25+
type Output = [T];
26+
#[inline]
27+
fn index(&self, index: ::core::ops::Range<usize>) -> &Self::Output {
28+
::core::ops::Index::index(&**self, index)
29+
}
30+
}
31+
32+
impl<$T> ::core::ops::IndexMut<::core::ops::Range<usize>> for $MemoryType
33+
{
34+
#[inline]
35+
fn index_mut(&mut self, index: ::core::ops::Range<usize>) -> &mut Self::Output {
36+
::core::ops::IndexMut::index_mut(&mut **self, index)
37+
}
38+
}
39+
40+
41+
impl<$T> ::core::ops::Deref for $MemoryType {
42+
type Target = [T];
43+
44+
fn deref(&self) -> &[T] {
45+
self.slice()
46+
}
47+
}
48+
49+
impl<T> ::core::ops::DerefMut for $MemoryType {
50+
fn deref_mut(&mut self) -> &mut [T] {
51+
self.slice_mut()
52+
}
53+
}
54+
};
55+
($T0: ident, $T:ident, $MemoryType:ty) => {
56+
impl<'a, $T> ::core::ops::Index<usize> for $MemoryType
57+
{
58+
type Output = T;
59+
60+
#[inline]
61+
fn index(&self, index: usize) -> &Self::Output {
62+
::core::ops::Index::index(&**self, index)
63+
}
64+
}
65+
66+
impl<'a, $T> ::core::ops::IndexMut<usize> for $MemoryType
67+
{
68+
#[inline]
69+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
70+
::core::ops::IndexMut::index_mut(&mut **self, index)
71+
}
72+
}
73+
74+
75+
impl<'a, $T> ::core::ops::Index<::core::ops::Range<usize>> for $MemoryType
76+
{
77+
type Output = [T];
78+
79+
#[inline]
80+
fn index(&self, index: ::core::ops::Range<usize>) -> &Self::Output {
81+
::core::ops::Index::index(&**self, index)
82+
}
83+
}
84+
85+
86+
impl<'a, $T> ::core::ops::IndexMut<::core::ops::Range<usize>> for $MemoryType
87+
{
88+
#[inline]
89+
fn index_mut(&mut self, index: ::core::ops::Range<usize>) -> &mut Self::Output {
90+
::core::ops::IndexMut::index_mut(&mut **self, index)
91+
}
92+
}
93+
94+
95+
impl<'a, $T> ::core::ops::Deref for $MemoryType {
96+
type Target = [T];
97+
98+
fn deref(&self) -> &[T] {
99+
self.slice()
100+
}
101+
}
102+
impl<'a, $T> ::core::ops::DerefMut for $MemoryType {
103+
fn deref_mut(&mut self) -> &mut [T] {
104+
self.slice_mut()
105+
}
106+
}
107+
}
108+
}
109+
#[macro_export]
110+
macro_rules! define_index_ops {
111+
($T:ident, $MemoryType:ty) => {
112+
impl<$T> ::core::ops::Index<usize> for $MemoryType
113+
{
114+
type Output = T;
115+
116+
#[inline]
117+
fn index(&self, index: usize) -> &Self::Output {
118+
::core::ops::Index::index(&**self, index)
119+
}
120+
}
121+
122+
123+
impl<$T> ::core::ops::Index<::core::ops::Range<usize>> for $MemoryType
124+
{
125+
type Output = [T];
126+
127+
#[inline]
128+
fn index(&self, index: ::core::ops::Range<usize>) -> &Self::Output {
129+
::core::ops::Index::index(&**self, index)
130+
}
131+
}
132+
133+
impl<$T> ::core::ops::Deref for $MemoryType {
134+
type Target = [T];
135+
136+
fn deref(&self) -> &[T] {
137+
self.slice()
138+
}
139+
}
140+
141+
};
142+
($T0: tt, $T:ident, $MemoryType:ty) => {
143+
impl<'a, $T> ::core::ops::Index<usize> for $MemoryType
144+
{
145+
type Output = T;
146+
147+
#[inline]
148+
fn index(&self, index: usize) -> &Self::Output {
149+
::core::ops::Index::index(&**self, index)
150+
}
151+
}
152+
153+
impl<'a, $T> ::core::ops::Index<::core::ops::Range<usize>> for $MemoryType
154+
{
155+
type Output = [T];
156+
157+
#[inline]
158+
fn index(&self, index: ::core::ops::Range<usize>) -> &Self::Output {
159+
::core::ops::Index::index(&**self, index)
160+
}
161+
}
162+
163+
164+
impl<'a, $T> ::core::ops::Deref for $MemoryType {
165+
type Target = [T];
166+
167+
fn deref(&self) -> &[T] {
168+
self.slice()
169+
}
170+
}
171+
}
172+
}

src/allocated_memory/test.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#![cfg(not(feature="no-stdlib"))]
2+
#![cfg(test)]
3+
4+
use super::{SliceWrapperMut, SliceWrapper};
5+
use std::vec::Vec;
6+
use core;
7+
use super::super::Allocator;
8+
#[derive(Debug)]
9+
pub struct ItemVec<Item>(Vec<Item>);
10+
define_index_ops_mut!(T, ItemVec<T>);
11+
12+
impl<Item> Default for ItemVec<Item> {
13+
fn default() -> Self {
14+
ItemVec(Vec::<Item>::new())
15+
}
16+
}
17+
impl<Item> SliceWrapper<Item> for ItemVec<Item> {
18+
fn slice(&self) -> &[Item] {
19+
&self.0[..]
20+
}
21+
}
22+
23+
impl<Item> SliceWrapperMut<Item> for ItemVec<Item> {
24+
fn slice_mut(&mut self) -> &mut [Item] {
25+
&mut self.0[..]
26+
}
27+
}
28+
/*
29+
impl<Item> core::ops::Index<usize> for ItemVec<Item> {
30+
type Output = Item;
31+
fn index(&self, index:usize) -> &Item {
32+
&self.0[index]
33+
}
34+
}
35+
36+
impl<Item> core::ops::IndexMut<usize> for ItemVec<Item> {
37+
38+
fn index_mut(&mut self, index:usize) -> &mut Item {
39+
&mut self.0[index]
40+
}
41+
}
42+
*/
43+
#[derive(Default)]
44+
struct ItemVecAllocator<Item> {
45+
_item: core::marker::PhantomData<Item>,
46+
}
47+
impl<Item:Default+Clone> Allocator<Item> for ItemVecAllocator<Item> {
48+
type AllocatedMemory = ItemVec<Item>;
49+
fn alloc_cell(&mut self, size:usize) ->ItemVec<Item>{
50+
//eprint!("A:{}\n", size);
51+
ItemVec(vec![Item::default();size])
52+
}
53+
fn free_cell(&mut self, _bv:ItemVec<Item>) {
54+
//eprint!("F:{}\n", _bv.slice().len());
55+
}
56+
}
57+
58+
59+
60+
#[derive(Copy,Clone)]
61+
pub struct SliceReference<'a, T:'a> {
62+
data: &'a[T],
63+
start: usize,
64+
len: usize,
65+
}
66+
67+
impl<'a, T:'a> SliceReference<'a, T> {
68+
pub fn new(input: &'a[T], start: usize, len: usize) -> SliceReference<'a, T> {
69+
SliceReference::<T> {
70+
data: input.split_at(start).1.split_at(len).0,
71+
start: start,
72+
len: len,
73+
}
74+
}
75+
}
76+
77+
impl<'a, T:'a> SliceWrapper<T> for SliceReference<'a, T> {
78+
fn slice(&self) -> &[T]{
79+
self.data
80+
}
81+
}
82+
83+
impl<'a, T> Default for SliceReference<'a, T> {
84+
fn default() ->SliceReference<'a, T> {
85+
SliceReference::<T> {
86+
data:&[],
87+
start:0,
88+
len:0,
89+
}
90+
}
91+
}
92+
93+
define_index_ops!(a, T, SliceReference<'a, T>);
94+
95+
#[test]
96+
fn test_index_ops() {
97+
let array = [255u8, 0u8, 1u8,2u8,3u8,4u8,5u8, 6u8];
98+
let sl = SliceReference::<u8>::new(&array[..], 1, 5);
99+
let val = sl[0];
100+
assert_eq!(val, 0);
101+
assert_eq!(&sl[1..5], &[1u8,2u8,3u8,4u8]);
102+
let mut al =ItemVecAllocator::<u64>::default();
103+
let mut dat = al.alloc_cell(1024);
104+
dat[0] = 0;
105+
dat[1] = 1;
106+
dat[2] = 2;
107+
dat[3] = 3;
108+
assert_eq!(dat[1], 1);
109+
assert_eq!(&dat[1..5], &[1u64,2u64,3u64,0u64]);
110+
assert_eq!(dat.len(), 1024);
111+
al.free_cell(dat);
112+
113+
}

0 commit comments

Comments
 (0)