-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharr.rs
69 lines (60 loc) · 1.47 KB
/
arr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use core::ops::{ Index, IndexMut };
use core::mem::size_of;
use ptr::Ptr;
use alloc::{ alloc, free };
pub struct Arr<T: Sized> {
ptr: Ptr<T>,
len: u32,
}
impl<T: Sized> Arr<T> {
pub fn new(len: u32) -> Arr<T> {
unsafe {
let data_ptr: Ptr<T> = alloc::<T>(len);
Arr {
ptr: data_ptr,
len: len * size_of::<T>() as u32,
}
}
}
pub fn len(&self) -> u32 { self.len }
pub fn free(mut self) {
unsafe {
free(&mut self.ptr);
}
}
}
impl<Item, Ind> Index<Ind> for Arr<Item>
where Item: Sized,
Ind: Sized + Into<u32> {
type Output = Item;
fn index(&self, index: Ind) -> &Item {
let i: u32 = index.into();
unsafe {
let x = Ptr::<Item>::from_u32(self.ptr.num + i * size_of::<Item>() as u32);
let y: &mut Item;
asm!("mov $0, $1"
: "=&r" (y)
: "r"(x)
:
: );
y
}
}
}
impl<Item, Ind> IndexMut<Ind> for Arr<Item>
where Item: Sized,
Ind: Sized + Into<u32> {
fn index_mut(&mut self, index: Ind) -> &mut Item {
let i: u32 = index.into();
unsafe {
let x = Ptr::<Item>::from_u32(self.ptr.num + i * size_of::<Item>() as u32);
let y: &mut Item;
asm!("mov $0, $1"
: "=&r" (y)
: "r"(x)
:
: );
y
}
}
}