-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuffer.rs
More file actions
59 lines (52 loc) · 1.63 KB
/
buffer.rs
File metadata and controls
59 lines (52 loc) · 1.63 KB
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
use core::ops::Range;
use core::ptr;
pub(crate) trait CopyFromNotOverlap<T> {
fn copy_from_not_overlap(&mut self, buffer: &[T]);
fn copy_to_range_from_not_overlap(&mut self, buffer: &[T], range: Range<usize>);
}
pub(crate) trait CopyNotOverlapValue<T> {
fn copy_value_from(&mut self, src: &[T], index: usize);
}
impl<T: Copy> CopyNotOverlapValue<T> for [T] {
#[inline(always)]
fn copy_value_from(&mut self, src: &[T], index: usize) {
unsafe {
ptr::copy_nonoverlapping(src.as_ptr().add(index), self.as_mut_ptr().add(index), 1);
}
}
}
impl<T> CopyFromNotOverlap<T> for [T] {
#[inline(always)]
fn copy_from_not_overlap(&mut self, buffer: &[T]) {
unsafe {
ptr::copy_nonoverlapping(buffer.as_ptr(), self.as_mut_ptr(), self.len());
}
}
#[inline(always)]
fn copy_to_range_from_not_overlap(&mut self, buffer: &[T], range: Range<usize>) {
debug_assert_eq!(range.len(), buffer.len());
let dst = unsafe { self.get_unchecked_mut(range) };
dst.copy_from_not_overlap(buffer);
}
}
pub(crate) trait DoubleRangeSlices<T> {
fn mut_slices<'a>(
&self,
slice1: &'a mut [T],
slice2: &'a mut [T],
) -> (&'a mut [T], &'a mut [T]);
}
impl<T> DoubleRangeSlices<T> for Range<usize> {
#[inline(always)]
fn mut_slices<'a>(
&self,
slice1: &'a mut [T],
slice2: &'a mut [T],
) -> (&'a mut [T], &'a mut [T]) {
unsafe {
let sub1 = slice1.get_unchecked_mut(self.clone());
let sub2 = slice2.get_unchecked_mut(self.clone());
(sub1, sub2)
}
}
}