Skip to content

Commit 41bc70f

Browse files
committed
Bump MSRV to 1.88 and use 2024 edition
1 parent d35404d commit 41bc70f

8 files changed

Lines changed: 99 additions & 91 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
toolchain:
18-
- "1.74"
18+
- "1.88"
1919
- stable
2020
- nightly
2121
steps:
@@ -27,19 +27,6 @@ jobs:
2727
- name: Setup sccache-cache
2828
uses: mozilla-actions/sccache-action@v0.0.9
2929

30-
# MSRV backfill: pyo3 0.28 requires rustc >=1.83 and pyo3 0.27 is
31-
# the last line with MSRV 1.74. dlpk accepts `>=0.27, <0.29`, so we
32-
# pin the pyo3 stack to the 0.27 line for the 1.74 job only. Stable
33-
# and nightly resolve to 0.28 naturally.
34-
- name: Pin pyo3 to 0.27 on MSRV toolchain
35-
if: matrix.toolchain == '1.74'
36-
run: |
37-
cargo update -p pyo3 --precise 0.27.2
38-
cargo update -p pyo3-build-config --precise 0.27.2
39-
cargo update -p pyo3-macros --precise 0.27.2
40-
cargo update -p pyo3-macros-backend --precise 0.27.2
41-
cargo update -p pyo3-ffi --precise 0.27.2
42-
4330
- name: Run tests
4431
env:
4532
SCCACHE_GHA_ENABLED: "true"

Cargo.toml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "dlpk"
33
version = "0.3.1"
4-
edition = "2021"
5-
rust-version = "1.74"
4+
edition = "2024"
5+
rust-version = "1.88"
66
license = "MIT OR Apache-2.0"
77

88
authors = [
@@ -18,16 +18,9 @@ keywords = ["dlpack", "deep-learning", "machine-learning"]
1818

1919
[dependencies]
2020
ndarray = { version = "0.17", optional = true }
21-
# Accept either 0.27 or 0.28 so downstream crates on either line can take
22-
# dlpk as a dep without forcing a pyo3 version conflict. 0.28 has MSRV 1.83
23-
# while 0.27 keeps MSRV 1.74; the MSRV CI job uses `cargo update --precise`
24-
# to pin to 0.27.
25-
pyo3 = { version = ">=0.27, <0.29", optional = true }
21+
pyo3 = { version = "0.29", optional = true }
2622
ouroboros = { version = "0.18", optional = true }
27-
28-
# 2.3 has MSRV 1.7.0
29-
# 2.5 has MSRV 1.81
30-
half = { version = "<2.5", optional = true }
23+
half = { version = "2.7", optional = true }
3124

3225
[features]
3326
ndarray = ["dep:ndarray"]

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,15 @@ impl std::fmt::Debug for DLPackTensor {
232232
flags_strings.join(" | ")
233233
};
234234

235+
let debug_tensor = unsafe {
236+
DLTensorDebug(&self.raw.as_ref().dl_tensor)
237+
};
235238
f.debug_struct("DLPackTensor")
236239
.field("version", unsafe { &self.raw.as_ref().version })
237240
.field("manager_ctx", unsafe { &self.raw.as_ref().manager_ctx })
238241
.field("deleter", unsafe { &self.raw.as_ref().deleter })
239242
.field("flags", &flags_string)
240-
.field("dl_tensor", unsafe { &DLTensorDebug(&self.raw.as_ref().dl_tensor) })
243+
.field("dl_tensor", &debug_tensor)
241244
.finish()
242245
}
243246
}
@@ -262,7 +265,9 @@ impl DLPackTensor {
262265
pub unsafe fn from_ptr(tensor: *mut sys::DLManagedTensorVersioned) -> DLPackTensor {
263266
let tensor = NonNull::new(tensor).expect("DLManagedTensorVersioned pointer is null");
264267

265-
return DLPackTensor::from_raw(tensor);
268+
return unsafe {
269+
DLPackTensor::from_raw(tensor)
270+
};
266271
}
267272

268273
/// Create a `DLPackTensor` from a non-null pointer to
@@ -272,15 +277,18 @@ impl DLPackTensor {
272277
///
273278
/// The same safety requirements as `from_ptr` apply.
274279
pub unsafe fn from_raw(tensor: NonNull<sys::DLManagedTensorVersioned>) -> DLPackTensor {
275-
if tensor.as_ref().version.major != sys::DLPACK_MAJOR_VERSION {
280+
let tensor_ref = unsafe { tensor.as_ref() };
281+
if tensor_ref.version.major != sys::DLPACK_MAJOR_VERSION {
276282
// from the spec, we need to call the deleter here (and it is the
277283
// only thing we can do)
278-
if let Some(deleter) = tensor.as_ref().deleter {
279-
deleter(tensor.as_ptr());
284+
if let Some(deleter) = tensor_ref.deleter {
285+
unsafe {
286+
deleter(tensor.as_ptr());
287+
}
280288
}
281289
panic!(
282290
"Incompatible DLPack version, got {}, but this code only supports version {}",
283-
tensor.as_ref().version.major, sys::DLPACK_MAJOR_VERSION
291+
tensor_ref.version.major, sys::DLPACK_MAJOR_VERSION
284292
);
285293
}
286294
return DLPackTensor{

src/ndarray/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,14 @@ struct ManagerContext<T> {
403403
}
404404

405405
unsafe extern "C" fn deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) {
406-
// Reconstruct the box and drop it, freeing the memory.
407-
let ctx = (*tensor).manager_ctx.cast::<ManagerContext<T>>();
408-
let _ = Box::from_raw(ctx);
406+
unsafe {
407+
// Reconstruct the box and drop it, freeing the memory.
408+
let ctx = (*tensor).manager_ctx.cast::<ManagerContext<T>>();
409+
let _ = Box::from_raw(ctx);
409410

410-
// also drop the tensor itself
411-
let _ = Box::from_raw(tensor);
411+
// also drop the tensor itself
412+
let _ = Box::from_raw(tensor);
413+
}
412414
}
413415

414416
impl<T, D> TryFrom<Array<T, D>> for DLPackTensor
@@ -801,7 +803,9 @@ mod tests {
801803
}
802804

803805
unsafe extern "C" fn box_deleter(tensor: *mut sys::DLManagedTensorVersioned) {
804-
let _ = Box::from_raw(tensor);
806+
unsafe {
807+
let _ = Box::from_raw(tensor);
808+
}
805809
}
806810

807811
#[test]

src/ndarray/sync.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ struct RwLockCtxRead<Array> where Array: 'static {
4646
}
4747

4848
unsafe extern "C" fn rwlock_write_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
49-
// Reconstruct the box and drop it, freeing the memory.
50-
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxWrite<T>>();
51-
let _ = Box::from_raw(ctx);
49+
unsafe {
50+
// Reconstruct the box and drop it, freeing the memory.
51+
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxWrite<T>>();
52+
let _ = Box::from_raw(ctx);
5253

53-
// also drop the tensor itself
54-
let _ = Box::from_raw(tensor);
54+
// also drop the tensor itself
55+
let _ = Box::from_raw(tensor);
56+
}
5557
}
5658

5759
unsafe extern "C" fn rwlock_read_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
58-
// Reconstruct the box and drop it, freeing the memory.
59-
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxRead<T>>();
60-
let _ = Box::from_raw(ctx);
60+
unsafe {
61+
// Reconstruct the box and drop it, freeing the memory.
62+
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxRead<T>>();
63+
let _ = Box::from_raw(ctx);
6164

62-
// also drop the tensor itself
63-
let _ = Box::from_raw(tensor);
65+
// also drop the tensor itself
66+
let _ = Box::from_raw(tensor);
67+
}
6468
}
6569

6670
impl<T, D> TryFrom<ReadWrite<Arc<RwLock<Array<T, D>>>>> for DLPackTensor
@@ -227,12 +231,14 @@ struct MutexCtx<Array> where Array: 'static {
227231
}
228232

229233
unsafe extern "C" fn mutex_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
230-
// Reconstruct the box and drop it, freeing the memory.
231-
let ctx = (*tensor).manager_ctx.cast::<MutexCtx<T>>();
232-
let _ = Box::from_raw(ctx);
234+
unsafe {
235+
// Reconstruct the box and drop it, freeing the memory.
236+
let ctx = (*tensor).manager_ctx.cast::<MutexCtx<T>>();
237+
let _ = Box::from_raw(ctx);
233238

234-
// also drop the tensor itself
235-
let _ = Box::from_raw(tensor);
239+
// also drop the tensor itself
240+
let _ = Box::from_raw(tensor);
241+
}
236242
}
237243

238244
impl<T, D> TryFrom<Arc<Mutex<Array<T, D>>>> for DLPackTensor

src/pyo3.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,8 @@ impl PyDLPack {
156156
// we can ignore `max_version`, the consumer is supposed to check it again
157157
// anyway
158158

159-
if let Some(device) = dl_device {
160-
if device.ne(self.__dlpack_device__(py)?)? {
161-
return Err(PyErr::new::<PyBufferError, _>("unsupported `dl_device`"));
162-
}
159+
if let Some(device) = dl_device && device.ne(self.__dlpack_device__(py)?)? {
160+
return Err(PyErr::new::<PyBufferError, _>("unsupported `dl_device`"));
163161
}
164162

165163
if copy.is_some() {
@@ -279,23 +277,25 @@ impl<'py> TryFrom<Bound<'py, PyCapsule>> for DLPackTensorRef<'py> {
279277
}
280278

281279
unsafe extern "C" fn rust_capsule_deleter(object: *mut pyo3::ffi::PyObject) {
282-
if pyo3::ffi::PyCapsule_IsValid(object, USED_DLTENSOR_VERSIONED_NAME.as_ptr()) == 1 {
283-
// All good, the data was already transfered
284-
return;
285-
}
280+
unsafe {
281+
if pyo3::ffi::PyCapsule_IsValid(object, USED_DLTENSOR_VERSIONED_NAME.as_ptr()) == 1 {
282+
// All good, the data was already transfered
283+
return;
284+
}
286285

287-
if !pyo3::ffi::PyCapsule_IsValid(object, DLTENSOR_VERSIONED_NAME.as_ptr()) == 1 {
288-
// we got a bad capsule, send a warning
289-
pyo3::ffi::PyErr_WriteUnraisable(object);
290-
return;
291-
}
286+
if !pyo3::ffi::PyCapsule_IsValid(object, DLTENSOR_VERSIONED_NAME.as_ptr()) == 1 {
287+
// we got a bad capsule, send a warning
288+
pyo3::ffi::PyErr_WriteUnraisable(object);
289+
return;
290+
}
292291

293-
let ptr = pyo3::ffi::PyCapsule_GetPointer(object, DLTENSOR_VERSIONED_NAME.as_ptr());
292+
let ptr = pyo3::ffi::PyCapsule_GetPointer(object, DLTENSOR_VERSIONED_NAME.as_ptr());
294293

295-
// PyCapsule_IsValid checks the the pointer is not null
296-
let tensor = NonNull::new(ptr.cast::<DLManagedTensorVersioned>())
297-
.expect("the capsule should be non-null");
298-
std::mem::drop(DLPackTensor::from_raw(tensor));
294+
// PyCapsule_IsValid checks the the pointer is not null
295+
let tensor = NonNull::new(ptr.cast::<DLManagedTensorVersioned>())
296+
.expect("the capsule should be non-null");
297+
std::mem::drop(DLPackTensor::from_raw(tensor));
298+
}
299299
}
300300

301301
impl TryFrom<DLPackTensor> for PyDLPack {

src/sync.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ struct MutexCtx<T> where T: 'static {
3838
}
3939

4040
unsafe extern "C" fn mutex_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
41-
// Reconstruct the box and drop it, freeing the memory.
42-
let ctx = (*tensor).manager_ctx.cast::<MutexCtx<T>>();
43-
let _ = Box::from_raw(ctx);
41+
unsafe {
42+
// Reconstruct the box and drop it, freeing the memory.
43+
let ctx = (*tensor).manager_ctx.cast::<MutexCtx<T>>();
44+
let _ = Box::from_raw(ctx);
4445

45-
// also drop the tensor itself
46-
let _ = Box::from_raw(tensor);
46+
// also drop the tensor itself
47+
let _ = Box::from_raw(tensor);
48+
}
4749
}
4850

4951
impl<T> TryFrom<Arc<Mutex<Vec<T>>>> for DLPackTensor where T: GetDLPackDataType + 'static {
@@ -132,21 +134,25 @@ struct RwLockCtxWrite<T> where T: 'static {
132134
}
133135

134136
unsafe extern "C" fn rwlock_read_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
135-
// Reconstruct the box and drop it, freeing the memory.
136-
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxRead<T>>();
137-
let _ = Box::from_raw(ctx);
137+
unsafe {
138+
// Reconstruct the box and drop it, freeing the memory.
139+
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxRead<T>>();
140+
let _ = Box::from_raw(ctx);
138141

139-
// also drop the tensor itself
140-
let _ = Box::from_raw(tensor);
142+
// also drop the tensor itself
143+
let _ = Box::from_raw(tensor);
144+
}
141145
}
142146

143147
unsafe extern "C" fn rwlock_write_deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) where T: 'static {
144-
// Reconstruct the box and drop it, freeing the memory.
145-
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxWrite<T>>();
146-
let _ = Box::from_raw(ctx);
148+
unsafe {
149+
// Reconstruct the box and drop it, freeing the memory.
150+
let ctx = (*tensor).manager_ctx.cast::<RwLockCtxWrite<T>>();
151+
let _ = Box::from_raw(ctx);
147152

148-
// also drop the tensor itself
149-
let _ = Box::from_raw(tensor);
153+
// also drop the tensor itself
154+
let _ = Box::from_raw(tensor);
155+
}
150156
}
151157

152158
impl<T> TryFrom<ReadWrite<Arc<RwLock<Vec<T>>>>> for DLPackTensor where T: GetDLPackDataType + 'static {

src/vec.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ struct ManagerContext<T> {
159159
}
160160

161161
unsafe extern "C" fn deleter_fn<T>(tensor: *mut sys::DLManagedTensorVersioned) {
162-
// Reconstruct the box and drop it, freeing the memory.
163-
let ctx = (*tensor).manager_ctx.cast::<ManagerContext<T>>();
164-
let _ = Box::from_raw(ctx);
162+
unsafe {
163+
// Reconstruct the box and drop it, freeing the memory.
164+
let ctx = (*tensor).manager_ctx.cast::<ManagerContext<T>>();
165+
let _ = Box::from_raw(ctx);
165166

166-
// also drop the tensor itself
167-
let _ = Box::from_raw(tensor);
167+
// also drop the tensor itself
168+
let _ = Box::from_raw(tensor);
169+
}
168170
}
169171

170172
macro_rules! impl_try_from {
@@ -316,7 +318,9 @@ mod tests {
316318
}
317319

318320
unsafe extern "C" fn box_deleter(tensor: *mut sys::DLManagedTensorVersioned) {
319-
let _ = Box::from_raw(tensor);
321+
unsafe {
322+
let _ = Box::from_raw(tensor);
323+
}
320324
}
321325

322326
#[test]

0 commit comments

Comments
 (0)