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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::{Builder, Error, build::finish};
use crate::{
ManagedBox, ManagedTensorBase, OpaqueContext,
metadata::{BorrowedArray, BorrowedSlice},
};
impl<C, const N: usize> Builder<C, BorrowedArray<'_, N>>
where
C: OpaqueContext,
{
/// Builds the tensor and transfers ownership to a raw DLPack pointer.
///
/// # Safety
///
/// The borrowed arrays must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn build_raw<M>(self) -> *mut M
where
M: ManagedTensorBase,
{
let Self {
ctx,
metadata,
fields,
} = self;
let managed = unsafe { metadata.allocate::<C, M>(ctx) };
unsafe { finish(managed, fields) }.as_ptr()
}
/// Builds a tensor that points to caller-owned shape and strides arrays.
///
/// # Safety
///
/// The borrowed arrays must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn build<M>(self) -> ManagedBox<M>
where
M: ManagedTensorBase,
{
unsafe { ManagedBox::new_unchecked(self.build_raw()) }
}
/// Tries to build the tensor and transfer ownership to a raw DLPack
/// pointer.
///
/// # Safety
///
/// The borrowed arrays must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn try_build_raw<M>(self) -> Result<*mut M, std::convert::Infallible>
where
M: ManagedTensorBase,
{
Ok(unsafe { self.build_raw() })
}
/// Tries to build a tensor that points to caller-owned shape and strides
/// arrays.
///
/// # Safety
///
/// The borrowed arrays must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn try_build<M>(self) -> Result<ManagedBox<M>, std::convert::Infallible>
where
M: ManagedTensorBase,
{
Ok(unsafe { self.build() })
}
}
impl<C> Builder<C, BorrowedSlice<'_>>
where
C: OpaqueContext,
{
/// Tries to build the tensor and transfer ownership to a raw DLPack
/// pointer.
///
/// # Safety
///
/// The borrowed slices must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn try_build_raw<M>(self) -> Result<*mut M, Error>
where
M: ManagedTensorBase,
{
let Self {
ctx,
metadata,
fields,
} = self;
let managed = unsafe { metadata.allocate::<C, M>(ctx)? };
Ok(unsafe { finish(managed, fields) }.as_ptr())
}
/// Builds a tensor that points to caller-owned shape and strides slices.
///
/// # Safety
///
/// The borrowed slices must outlive the returned managed tensor and must
/// not be mutated through the DLPack `shape`/`strides` pointers while it
/// is alive.
#[inline]
pub unsafe fn try_build<M>(self) -> Result<ManagedBox<M>, Error>
where
M: ManagedTensorBase,
{
unsafe { self.try_build_raw() }.map(|raw| unsafe { ManagedBox::new_unchecked(raw) })
}
/// Builds the tensor without checking runtime metadata invariants.
///
/// # Safety
///
/// The borrowed slices must outlive the returned managed tensor, and shape
/// and strides must have the same length with `ndim` fitting in `i32`.
/// They must not be mutated through the DLPack `shape`/`strides` pointers
/// while the managed tensor is alive.
#[inline]
pub unsafe fn build_raw_unchecked<M>(self) -> *mut M
where
M: ManagedTensorBase,
{
let Self {
ctx,
metadata,
fields,
} = self;
let managed = unsafe { metadata.allocate_unchecked::<C, M>(ctx) };
unsafe { finish(managed, fields) }.as_ptr()
}
/// Builds a tensor that points to caller-owned shape and strides slices
/// without checking runtime metadata invariants.
///
/// # Safety
///
/// The borrowed slices must outlive the returned managed tensor, and shape
/// and strides must have the same length with `ndim` fitting in `i32`.
/// They must not be mutated through the DLPack `shape`/`strides` pointers
/// while the managed tensor is alive.
#[inline]
pub unsafe fn build_unchecked<M>(self) -> ManagedBox<M>
where
M: ManagedTensorBase,
{
unsafe { ManagedBox::new_unchecked(self.build_raw_unchecked()) }
}
}