-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathslice_container.rs
120 lines (102 loc) · 3.3 KB
/
slice_container.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::{mem, ptr};
use ndarray::{ArrayBase, Dimension, OwnedRepr};
use pyo3::pyclass;
/// Utility type to safely store `Box<[_]>` or `Vec<_>` on the Python heap
#[pyclass(frozen)]
#[derive(Debug)]
pub(crate) struct PySliceContainer {
pub(crate) ptr: *mut u8,
pub(crate) len: usize,
cap: usize,
drop: unsafe fn(*mut u8, usize, usize),
}
// This resembles `unsafe impl<T: Send> Send for PySliceContainer<T> {}` if we
// were allow to use a generic there.
// SAFETY: Every construction below enforces `T: Send` fulfilling the ideal bound above
unsafe impl Send for PySliceContainer {}
// This resembles `unsafe impl<T: Sync> Sync for PySliceContainer<T> {}` if we
// were allow to use a generic there.
// SAFETY: Every construction below enforces `T: Sync` fulfilling the ideal bound above
unsafe impl Sync for PySliceContainer {}
impl<T: Send + Sync> From<Box<[T]>> for PySliceContainer {
fn from(data: Box<[T]>) -> Self {
unsafe fn drop_boxed_slice<T>(ptr: *mut u8, len: usize, _cap: usize) {
let _ = Box::from_raw(ptr::slice_from_raw_parts_mut(ptr as *mut T, len));
}
// FIXME(adamreichold): Use `Box::into_raw` when
// `*mut [T]::{as_mut_ptr, len}` become stable and compatible with our MSRV.
let mut data = mem::ManuallyDrop::new(data);
let ptr = data.as_mut_ptr() as *mut u8;
let len = data.len();
let cap = 0;
let drop = drop_boxed_slice::<T>;
Self {
ptr,
len,
cap,
drop,
}
}
}
impl<T: Send + Sync> From<Vec<T>> for PySliceContainer {
fn from(data: Vec<T>) -> Self {
unsafe fn drop_vec<T>(ptr: *mut u8, len: usize, cap: usize) {
let _ = Vec::from_raw_parts(ptr as *mut T, len, cap);
}
// FIXME(adamreichold): Use `Vec::into_raw_parts`
// when it becomes stable and compatible with our MSRV.
let mut data = mem::ManuallyDrop::new(data);
let ptr = data.as_mut_ptr() as *mut u8;
let len = data.len();
let cap = data.capacity();
let drop = drop_vec::<T>;
Self {
ptr,
len,
cap,
drop,
}
}
}
#[cfg(feature = "faer")]
impl<T: Send + Sync> From<faer::Mat<T>> for PySliceContainer {
fn from(data: faer::Mat<T>) -> Self {
unsafe fn drop_faer_mat<T>(ptr: *mut u8, len_nrows: usize, cap_ncols: usize) {
let _ = faer::mat::MatMut::from_raw_parts_mut(
ptr as *mut T,
len_nrows,
cap_ncols,
1,
cap_ncols as isize,
);
}
let mut data = mem::ManuallyDrop::new(data);
let ptr = data.as_ptr_mut() as *mut u8;
let len = data.nrows();
let cap = data.ncols();
let drop = drop_faer_mat::<T>;
Self {
ptr,
len,
cap,
drop,
}
}
}
impl<A, D> From<ArrayBase<OwnedRepr<A>, D>> for PySliceContainer
where
A: Send + Sync,
D: Dimension,
{
fn from(data: ArrayBase<OwnedRepr<A>, D>) -> Self {
#[allow(deprecated)]
Self::from(data.into_raw_vec())
}
}
impl Drop for PySliceContainer {
fn drop(&mut self) {
unsafe {
(self.drop)(self.ptr, self.len, self.cap);
}
}
}