minarrow 0.16.2

Apache Arrow-compatible, Rust-first columnar data library for high-performance computing, native streaming, and embedded workloads. Minimal dependencies, ultra-low-latency access, automatic 64-byte SIMD alignment, and fast compile times. Great for real-time analytics, HPC pipelines, and systems integration.
Documentation
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
//! # **DLPack FFI** - *Tensor interchange with Python and other runtimes*
//!
//! Implements the [DLPack](https://github.com/dmlc/dlpack) tensor interchange
//! standard for NdArray. Compatible ownership, alignment, and protocol choices
//! allow runtimes to share the allocation directly; the documented fallback
//! cases copy so that Minarrow's ownership and alignment guarantees still hold.
//!
//! ## Supported protocols
//! - **DLManagedTensor** (legacy, pre-1.0) - the capsule payload older
//!   consumers expect
//! - **DLManagedTensorVersioned** (DLPack 1.x) - carries the spec version
//!   and the flags word, including the read-only bit
//! - Export: [`export_to_dlpack`] / [`export_to_dlpack_versioned`] wrap an
//!   NdArray, and [`export_view_to_dlpack`] / [`export_view_to_dlpack_versioned`]
//!   wrap an NdArrayV window without copying
//! - Import: [`import_from_dlpack`] / [`import_from_dlpack_versioned`] wrap a
//!   foreign managed tensor as an NdArray
//!
//! ## Version negotiation
//! A Python `__dlpack__(max_version=...)` implementation calls
//! [`export_to_dlpack_versioned`] when the consumer requests major version 1
//! or above, and [`export_to_dlpack`] otherwise. The versioned capsule is
//! named `dltensor_versioned` and the legacy capsule `dltensor`. A consumed
//! capsule renames with a `used_` prefix per the protocol.
//!
//! ## Read-only flag
//! Versioned exports set `DLPACK_FLAG_BITMASK_READ_ONLY` when the backing
//! buffer is shared, since a consumer writing through the pointer would be
//! visible to every other reference. Imported read-only tensors land as
//! shared buffers, so Minarrow's copy-on-write semantics honour the flag -
//! the first mutation copies.
//!
//! ## Layout compatibility
//! NdArray's compact column-major layout is expressed through DLPack's
//! strides field, and the buffer is fully contiguous, so consumers receive
//! a dense tensor with no dead bytes. Consumers such as PyTorch, NumPy, JAX,
//! and TensorFlow decide whether to operate on those strides directly or
//! re-lay the tensor for a particular operation.
//!
//! ## Notes
//! - DLPack uses element strides, matching NdArray's convention
//! - DLPack has no null mask concept - NaN-based missing values align with this
//! - The allocation start is 64-byte aligned, matching the DLPack
//!   recommendation for aligned data pointers
//! - Imported foreign buffers that are not 64-byte aligned copy into an
//!   aligned `Vec64` so the crate's SIMD-alignment invariant holds

use std::ffi::c_void;
use std::sync::Arc;

use crate::enums::error::MinarrowError;
use crate::structs::buffer::Buffer;
use crate::structs::ndarray::NdArray;
use crate::structs::shared_buffer::SharedBuffer;
#[cfg(feature = "views")]
use crate::structs::views::ndarray_view::NdArrayV;
use crate::traits::type_unions::Float;

// ****************************************************************
// DLPack C structs (matching the DLPack header)
// ****************************************************************

/// The DLPack major version this crate produces and understands.
pub const DLPACK_MAJOR_VERSION: u32 = 1;

/// The DLPack minor version this crate produces.
pub const DLPACK_MINOR_VERSION: u32 = 1;

/// Flag bit marking the tensor data as read-only.
pub const DLPACK_FLAG_BITMASK_READ_ONLY: u64 = 1;

/// Flag bit marking the tensor as a copy of the producer's data.
pub const DLPACK_FLAG_BITMASK_IS_COPIED: u64 = 2;

/// Flag bit marking sub-byte element types as byte-padded.
pub const DLPACK_FLAG_BITMASK_IS_SUBBYTE_TYPE_PADDED: u64 = 4;

/// Device type code from the DLPack spec.
///
/// Held as a plain integer rather than a Rust enum, since a foreign
/// producer may send any code the spec defines, including ones added
/// after this crate was compiled. Reading an unknown discriminant into
/// a Rust enum would be undefined behaviour.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DLDeviceType(pub i32);

impl DLDeviceType {
    pub const CPU: Self = DLDeviceType(1);
    pub const CUDA: Self = DLDeviceType(2);
    pub const CUDA_HOST: Self = DLDeviceType(3);
    pub const OPENCL: Self = DLDeviceType(4);
    pub const VULKAN: Self = DLDeviceType(7);
    pub const METAL: Self = DLDeviceType(8);
    pub const VPI: Self = DLDeviceType(9);
    pub const ROCM: Self = DLDeviceType(10);
    pub const ROCM_HOST: Self = DLDeviceType(11);
    pub const EXT_DEV: Self = DLDeviceType(12);
    pub const CUDA_MANAGED: Self = DLDeviceType(13);
    pub const ONE_API: Self = DLDeviceType(14);
    pub const WEBGPU: Self = DLDeviceType(15);
    pub const HEXAGON: Self = DLDeviceType(16);
    pub const MAIA: Self = DLDeviceType(17);
}

/// Device descriptor.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DLDevice {
    pub device_type: DLDeviceType,
    pub device_id: i32,
}

impl DLDevice {
    /// Host CPU device, id 0. The device every Minarrow buffer lives on,
    /// and the value a Python `__dlpack_device__` implementation returns.
    pub const fn cpu() -> Self {
        DLDevice { device_type: DLDeviceType::CPU, device_id: 0 }
    }
}

/// Data type descriptor.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DLDataType {
    /// Type code: 0=int, 1=uint, 2=float, 4=bfloat, 5=complex, 6=bool
    pub code: u8,
    /// Number of bits per element
    pub bits: u8,
    /// Number of SIMD lanes, typically 1
    pub lanes: u16,
}

impl DLDataType {
    /// f32 type descriptor.
    pub const FLOAT32: Self = DLDataType { code: 2, bits: 32, lanes: 1 };

    /// f64 type descriptor.
    pub const FLOAT64: Self = DLDataType { code: 2, bits: 64, lanes: 1 };

    /// Float descriptor for a Minarrow element type.
    pub fn float<T: Float>() -> Self {
        DLDataType { code: 2, bits: (std::mem::size_of::<T>() * 8) as u8, lanes: 1 }
    }
}

/// DLPack spec version carried by the versioned managed tensor.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DLPackVersion {
    pub major: u32,
    pub minor: u32,
}

/// The core DLPack tensor descriptor. Does not own the data.
#[repr(C)]
pub struct DLTensor {
    /// Pointer to the start of the data buffer.
    pub data: *mut c_void,
    /// Device where the data resides.
    pub device: DLDevice,
    /// Number of dimensions.
    pub ndim: i32,
    /// Data type.
    pub dtype: DLDataType,
    /// Shape array with ndim elements.
    pub shape: *mut i64,
    /// Strides array with ndim elements, or null for C-contiguous.
    pub strides: *mut i64,
    /// Byte offset from data pointer to the start of actual data.
    pub byte_offset: u64,
}

/// DLPack legacy managed tensor with ownership semantics.
/// The deleter is called when the consumer is done with the tensor.
#[repr(C)]
pub struct DLManagedTensor {
    pub dl_tensor: DLTensor,
    pub manager_ctx: *mut c_void,
    pub deleter: Option<unsafe extern "C" fn(*mut DLManagedTensor)>,
}

unsafe impl Send for DLManagedTensor {}
unsafe impl Sync for DLManagedTensor {}

/// DLPack 1.x managed tensor. Extends the legacy layout with the spec
/// version and a flags word. Field order is ABI - version leads so a
/// consumer can check compatibility before reading the rest.
#[repr(C)]
pub struct DLManagedTensorVersioned {
    pub version: DLPackVersion,
    pub manager_ctx: *mut c_void,
    pub deleter: Option<unsafe extern "C" fn(*mut DLManagedTensorVersioned)>,
    pub flags: u64,
    pub dl_tensor: DLTensor,
}

unsafe impl Send for DLManagedTensorVersioned {}
unsafe impl Sync for DLManagedTensorVersioned {}

// ****************************************************************
// Export: NdArray / NdArrayV -> managed tensor
// ****************************************************************

/// Keeps the exported source and the i64 shape/strides arrays alive for
/// the lifetime of the managed tensor.
struct DLPackHolder<A> {
    _source: A,
    shape_i64: Vec<i64>,
    strides_i64: Vec<i64>,
}

/// Export an NdArray as a legacy DLPack managed tensor.
///
/// Legacy `DLManagedTensor` carries no read-only flag, so consumers
/// treat the pointer as writable. A uniquely owned Minarrow allocation
/// transfers zero-copy. Shared or aliased storage copies first so a
/// consumer cannot mutate another reference through the legacy protocol.
/// Consumers on the 1.x protocol should prefer `to_dlpack_versioned`,
/// which can share the buffer and carry its read-only state. A buffer
/// without a stable owned allocation also copies first, since the consumer
/// holds the base pointer for the tensor's whole lifetime.
///
/// Returns a [`DLPackTensor`] that manages the lifecycle. The NdArray's
/// buffer stays alive through its internal shared reference count. When
/// the owned tensor is dropped, the backing data is released.
///
/// For FFI handoff (e.g. creating a PyCapsule), call `.into_raw()` to
/// transfer ownership to the consumer.
pub fn export_to_dlpack<T: Float>(ndarray: NdArray<T>) -> DLPackTensor {
    let ndarray = if ndarray.data.is_owned() && Arc::strong_count(&ndarray.data) == 1 {
        ndarray
    } else {
        NdArray::from_buffer(ndarray.data.to_owned_copy(), ndarray.shape(), ndarray.strides())
    };
    let shape = ndarray.shape().to_vec();
    let strides = ndarray.strides().to_vec();
    let data_ptr = ndarray.as_slice().as_ptr() as *mut c_void;
    DLPackTensor::from_parts(ndarray, data_ptr, 0, DLDataType::float::<T>(), &shape, &strides)
}

/// Export an NdArray as a DLPack 1.x versioned managed tensor.
///
/// Sets `DLPACK_FLAG_BITMASK_READ_ONLY` when the backing buffer is
/// shared, since a consumer writing through the pointer would be visible
/// to every other reference. A uniquely owned buffer exports writable.
/// A buffer without a stable owned or shared allocation copies into an
/// owned one first, since the consumer holds the base pointer for the
/// tensor's whole lifetime.
pub fn export_to_dlpack_versioned<T: Float>(ndarray: NdArray<T>) -> DLPackTensorVersioned {
    let ndarray = if ndarray.data.is_owned() || ndarray.data.is_shared() {
        ndarray
    } else {
        NdArray::from_buffer(ndarray.data.to_owned_copy(), ndarray.shape(), ndarray.strides())
    };
    let read_only = Arc::strong_count(&ndarray.data) > 1 || ndarray.data.is_shared();
    let flags = if read_only { DLPACK_FLAG_BITMASK_READ_ONLY } else { 0 };
    let shape = ndarray.shape().to_vec();
    let strides = ndarray.strides().to_vec();
    let data_ptr = ndarray.as_slice().as_ptr() as *mut c_void;
    DLPackTensorVersioned::from_parts(
        ndarray, data_ptr, 0, DLDataType::float::<T>(), &shape, &strides, flags,
    )
}

/// Export an NdArrayV window as a legacy DLPack managed tensor.
///
/// Legacy `DLManagedTensor` carries no read-only flag, so a consumer
/// treats the pointer as writable. A view over a uniquely owned Minarrow
/// allocation exports zero-copy, carrying its offset and strides. A view
/// over shared or aliased storage materialises first so a legacy consumer
/// cannot mutate another reference. Consumers on the 1.x protocol should
/// prefer `to_dlpack_versioned`, which can share the backing and flag it
/// read-only.
#[cfg(feature = "views")]
pub fn export_view_to_dlpack<T: Float>(view: NdArrayV<T>) -> DLPackTensor {
    if !view.source.data.is_owned() || Arc::strong_count(&view.source.data) != 1 {
        return export_to_dlpack(view.to_ndarray());
    }
    let shape = view.shape().to_vec();
    let strides = view.strides().to_vec();
    let byte_offset = (view.offset * std::mem::size_of::<T>()) as u64;
    let data_ptr = view.source.as_slice().as_ptr() as *mut c_void;
    DLPackTensor::from_parts(view, data_ptr, byte_offset, DLDataType::float::<T>(), &shape, &strides)
}

/// Export an NdArrayV window as a DLPack 1.x versioned managed tensor
/// without copying. The window offset carries through DLPack's
/// `byte_offset` field, and the view strides carry as element strides,
/// so sliced, transposed, and permuted views all hand over zero-copy.
/// A view shares its source buffer, so the read-only flag is set
/// whenever another reference to that buffer exists. A backing without
/// a stable owned or shared allocation materialises the window first.
#[cfg(feature = "views")]
pub fn export_view_to_dlpack_versioned<T: Float>(view: NdArrayV<T>) -> DLPackTensorVersioned {
    if !(view.source.data.is_owned() || view.source.data.is_shared()) {
        return export_to_dlpack_versioned(view.to_ndarray());
    }
    let read_only = Arc::strong_count(&view.source.data) > 1 || view.source.data.is_shared();
    let flags = if read_only { DLPACK_FLAG_BITMASK_READ_ONLY } else { 0 };
    let shape = view.shape().to_vec();
    let strides = view.strides().to_vec();
    let byte_offset = (view.offset * std::mem::size_of::<T>()) as u64;
    let data_ptr = view.source.as_slice().as_ptr() as *mut c_void;
    DLPackTensorVersioned::from_parts(
        view, data_ptr, byte_offset, DLDataType::float::<T>(), &shape, &strides, flags,
    )
}

/// Release callback invoked by the foreign consumer when it is done
/// with the tensor. Drops the holder which releases the source.
///
/// # Safety
/// Must only be called once per DLManagedTensor.
unsafe extern "C" fn dlpack_deleter<A>(managed: *mut DLManagedTensor) {
    if managed.is_null() { return; }
    let managed = unsafe { Box::from_raw(managed) };
    if !managed.manager_ctx.is_null() {
        let _holder: Box<DLPackHolder<A>> = unsafe {
            Box::from_raw(managed.manager_ctx as *mut DLPackHolder<A>)
        };
        // holder drops here, releasing the source
    }
}

/// Release callback for the versioned managed tensor.
///
/// # Safety
/// Must only be called once per DLManagedTensorVersioned.
unsafe extern "C" fn dlpack_deleter_versioned<A>(managed: *mut DLManagedTensorVersioned) {
    if managed.is_null() { return; }
    let managed = unsafe { Box::from_raw(managed) };
    if !managed.manager_ctx.is_null() {
        let _holder: Box<DLPackHolder<A>> = unsafe {
            Box::from_raw(managed.manager_ctx as *mut DLPackHolder<A>)
        };
        // holder drops here, releasing the source
    }
}

// ****************************************************************
// DLPackTensor / DLPackTensorVersioned - safe wrappers with Drop
// ****************************************************************

/// Safe wrapper owning a legacy DLPack managed tensor. Calls the deleter
/// on drop.
///
/// This is the return type of `NdArray::to_dlpack()`, analogous to how
/// `to_apache_arrow()` returns an `ArrayRef` that owns its lifecycle.
pub struct DLPackTensor {
    ptr: *mut DLManagedTensor,
}

impl DLPackTensor {
    /// Assemble the managed tensor around a keep-alive source.
    fn from_parts<A>(
        source: A,
        data: *mut c_void,
        byte_offset: u64,
        dtype: DLDataType,
        shape: &[usize],
        strides: &[usize],
    ) -> Self {
        let mut holder = Box::new(DLPackHolder {
            _source: source,
            shape_i64: shape.iter().map(|&s| s as i64).collect(),
            strides_i64: strides.iter().map(|&s| s as i64).collect(),
        });
        let shape_ptr = if holder.shape_i64.is_empty() {
            std::ptr::null_mut()
        } else {
            holder.shape_i64.as_mut_ptr()
        };
        let strides_ptr = if holder.strides_i64.is_empty() {
            std::ptr::null_mut()
        } else {
            holder.strides_i64.as_mut_ptr()
        };
        let tensor = DLTensor {
            data,
            device: DLDevice::cpu(),
            ndim: shape.len() as i32,
            dtype,
            shape: shape_ptr,
            strides: strides_ptr,
            byte_offset,
        };
        let managed = Box::new(DLManagedTensor {
            dl_tensor: tensor,
            manager_ctx: Box::into_raw(holder) as *mut c_void,
            deleter: Some(dlpack_deleter::<A>),
        });
        DLPackTensor { ptr: Box::into_raw(managed) }
    }

    /// Raw pointer access for FFI consumers that need to take ownership
    /// e.g. when creating a PyCapsule.
    ///
    /// After calling this, the DLPackTensor no longer owns the pointer
    /// and will not call the deleter on drop. The caller is responsible for
    /// ensuring the deleter is called.
    pub fn into_raw(mut self) -> *mut DLManagedTensor {
        let ptr = self.ptr;
        self.ptr = std::ptr::null_mut();
        ptr
    }

    /// Access the underlying DLTensor for reading shape, strides, data.
    pub fn tensor(&self) -> &DLTensor {
        unsafe { &(*self.ptr).dl_tensor }
    }
}

impl Drop for DLPackTensor {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                if let Some(deleter) = (*self.ptr).deleter {
                    deleter(self.ptr);
                }
            }
        }
    }
}

/// Safe wrapper owning a DLPack 1.x versioned managed tensor. Calls the
/// deleter on drop.
///
/// This is the return type of `NdArray::to_dlpack_versioned()`, and the
/// payload a Python `__dlpack__` implementation places in a
/// `dltensor_versioned` capsule via `.into_raw()`.
pub struct DLPackTensorVersioned {
    ptr: *mut DLManagedTensorVersioned,
}

impl DLPackTensorVersioned {
    /// Assemble the versioned managed tensor around a keep-alive source.
    fn from_parts<A>(
        source: A,
        data: *mut c_void,
        byte_offset: u64,
        dtype: DLDataType,
        shape: &[usize],
        strides: &[usize],
        flags: u64,
    ) -> Self {
        let mut holder = Box::new(DLPackHolder {
            _source: source,
            shape_i64: shape.iter().map(|&s| s as i64).collect(),
            strides_i64: strides.iter().map(|&s| s as i64).collect(),
        });
        let shape_ptr = if holder.shape_i64.is_empty() {
            std::ptr::null_mut()
        } else {
            holder.shape_i64.as_mut_ptr()
        };
        let strides_ptr = if holder.strides_i64.is_empty() {
            std::ptr::null_mut()
        } else {
            holder.strides_i64.as_mut_ptr()
        };
        let tensor = DLTensor {
            data,
            device: DLDevice::cpu(),
            ndim: shape.len() as i32,
            dtype,
            shape: shape_ptr,
            strides: strides_ptr,
            byte_offset,
        };
        let managed = Box::new(DLManagedTensorVersioned {
            version: DLPackVersion {
                major: DLPACK_MAJOR_VERSION,
                minor: DLPACK_MINOR_VERSION,
            },
            manager_ctx: Box::into_raw(holder) as *mut c_void,
            deleter: Some(dlpack_deleter_versioned::<A>),
            flags,
            dl_tensor: tensor,
        });
        DLPackTensorVersioned { ptr: Box::into_raw(managed) }
    }

    /// Raw pointer access for FFI consumers that need to take ownership
    /// e.g. when creating a PyCapsule.
    ///
    /// After calling this, the DLPackTensorVersioned no longer owns the
    /// pointer and will not call the deleter on drop. The caller is
    /// responsible for ensuring the deleter is called.
    pub fn into_raw(mut self) -> *mut DLManagedTensorVersioned {
        let ptr = self.ptr;
        self.ptr = std::ptr::null_mut();
        ptr
    }

    /// Access the underlying DLTensor for reading shape, strides, data.
    pub fn tensor(&self) -> &DLTensor {
        unsafe { &(*self.ptr).dl_tensor }
    }

    /// The DLPack spec version stamped on this tensor.
    pub fn version(&self) -> DLPackVersion {
        unsafe { (*self.ptr).version }
    }

    /// The flags word, including `DLPACK_FLAG_BITMASK_READ_ONLY`.
    pub fn flags(&self) -> u64 {
        unsafe { (*self.ptr).flags }
    }
}

impl Drop for DLPackTensorVersioned {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                if let Some(deleter) = (*self.ptr).deleter {
                    deleter(self.ptr);
                }
            }
        }
    }
}

// ****************************************************************
// Import: managed tensor -> NdArray
// ****************************************************************

/// The foreign managed tensor behind an import, either ABI.
enum ForeignManaged {
    Legacy(*mut DLManagedTensor),
    Versioned(*mut DLManagedTensorVersioned),
}

/// Owns the foreign managed tensor and calls its deleter on drop.
///
/// The DLPack contract makes the deleter responsible for releasing the
/// managed tensor allocation, so the raw pointer is held rather than a
/// `Box`. Wrapping it in a `Box` would free the allocation a second time
/// after the deleter already released it.
struct ForeignDLPack {
    ptr: *const u8,
    len_bytes: usize,
    managed: ForeignManaged,
}

impl AsRef<[u8]> for ForeignDLPack {
    fn as_ref(&self) -> &[u8] {
        if self.len_bytes == 0 || self.ptr.is_null() {
            return &[];
        }
        unsafe { std::slice::from_raw_parts(self.ptr, self.len_bytes) }
    }
}

impl Drop for ForeignDLPack {
    fn drop(&mut self) {
        unsafe {
            match self.managed {
                ForeignManaged::Legacy(m) if !m.is_null() => {
                    if let Some(deleter) = (*m).deleter {
                        deleter(m);
                    }
                }
                ForeignManaged::Versioned(m) if !m.is_null() => {
                    if let Some(deleter) = (*m).deleter {
                        deleter(m);
                    }
                }
                _ => {}
            }
        }
        self.managed = ForeignManaged::Legacy(std::ptr::null_mut());
    }
}

unsafe impl Send for ForeignDLPack {}
unsafe impl Sync for ForeignDLPack {}

/// Validation and wrapping pipeline shared by both import entry points.
///
/// The guard owns the foreign tensor, so any validation failure drops it
/// and calls the foreign deleter as the ownership-transfer contract
/// requires. On the success path the guard moves into the SharedBuffer
/// and the deleter runs when the NdArray is dropped.
///
/// # Safety
/// The DLTensor must belong to the managed tensor held by `foreign` and
/// point to accessible CPU memory.
unsafe fn import_dl_tensor<T: Float>(
    tensor: &DLTensor,
    mut foreign: ForeignDLPack,
) -> Result<NdArray<T>, MinarrowError> {
    if tensor.device.device_type != DLDeviceType::CPU {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!(
                "only CPU tensors are supported, got device type {}",
                tensor.device.device_type.0
            ),
        });
    }

    let bits = (std::mem::size_of::<T>() * 8) as u8;
    if tensor.dtype.code != 2 || tensor.dtype.bits != bits || tensor.dtype.lanes != 1 {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!(
                "dtype mismatch, expected code=2 bits={}, got code={} bits={} lanes={}",
                bits, tensor.dtype.code, tensor.dtype.bits, tensor.dtype.lanes
            ),
        });
    }

    if tensor.ndim < 0 {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!("tensor rank cannot be negative, got ndim={}", tensor.ndim),
        });
    }
    let ndim = tensor.ndim as usize;

    if ndim > 0 && tensor.shape.is_null() {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!("null shape pointer for ndim={}", ndim),
        });
    }
    let raw_shape: &[i64] = if ndim == 0 {
        &[]
    } else {
        unsafe { std::slice::from_raw_parts(tensor.shape, ndim) }
    };
    if raw_shape.iter().any(|&s| s < 0) {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!("negative dimension in shape {:?}", raw_shape),
        });
    }
    let shape: Vec<usize> = raw_shape.iter().map(|&s| s as usize).collect();

    // Null strides mean C-contiguous i.e. row-major.
    let strides: Vec<usize> = if tensor.strides.is_null() {
        let mut s = vec![1usize; ndim];
        if ndim > 0 {
            for d in (0..ndim - 1).rev() {
                s[d] = s[d + 1] * shape[d + 1];
            }
        }
        s
    } else {
        let raw_strides = unsafe { std::slice::from_raw_parts(tensor.strides, ndim) };
        if raw_strides.iter().any(|&s| s < 0) {
            return Err(MinarrowError::BridgeError {
                source: "dlpack",
                message: format!(
                    "negative strides {:?} are not representable in NdArray",
                    raw_strides
                ),
            });
        }
        raw_strides.iter().map(|&s| s as usize).collect()
    };

    // Buffer length required to reach the last element, overflow-checked.
    let buf_len = if shape.iter().any(|&s| s == 0) {
        0
    } else {
        let mut max_offset = 0usize;
        for (&s, &st) in shape.iter().zip(strides.iter()) {
            let contribution = (s - 1).checked_mul(st).ok_or_else(|| MinarrowError::BridgeError {
                source: "dlpack",
                message: format!("shape {:?} with strides {:?} overflows the addressable range", shape, strides),
            })?;
            max_offset = max_offset.checked_add(contribution).ok_or_else(|| MinarrowError::BridgeError {
                source: "dlpack",
                message: format!("shape {:?} with strides {:?} overflows the addressable range", shape, strides),
            })?;
        }
        max_offset + 1
    };

    // The byte window the elements span, overflow-checked and capped at
    // isize::MAX per the slice safety contract.
    let len_bytes = buf_len
        .checked_mul(std::mem::size_of::<T>())
        .filter(|&n| n <= isize::MAX as usize)
        .ok_or_else(|| MinarrowError::BridgeError {
            source: "dlpack",
            message: format!(
                "shape {:?} with strides {:?} spans more than isize::MAX bytes",
                shape, strides
            ),
        })?;

    if tensor.data.is_null() && buf_len > 0 {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!("null data pointer for a tensor of {} elements", buf_len),
        });
    }

    // Data pointer with byte offset, checked for element alignment. The
    // 64-byte SIMD alignment is handled downstream - Buffer::from_shared
    // copies a non-aligned foreign buffer into an aligned Vec64.
    let data_ptr = if tensor.data.is_null() {
        std::ptr::null()
    } else {
        unsafe { (tensor.data as *const u8).add(tensor.byte_offset as usize) }
    };
    if buf_len > 0 && (data_ptr as usize) % std::mem::align_of::<T>() != 0 {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!("data pointer is not aligned for a {}-bit element", bits),
        });
    }

    // Record the data window now that shape and strides are validated.
    foreign.ptr = data_ptr;
    foreign.len_bytes = len_bytes;

    // Wrap into SharedBuffer -> Buffer -> NdArray. The guard moves into the
    // SharedBuffer, so the deleter now runs when the NdArray is dropped.
    let shared = SharedBuffer::from_owner(foreign);
    let buffer: Buffer<T> = Buffer::from_shared(shared);

    Ok(NdArray::from_buffer(buffer, &shape, &strides))
}

/// Import a foreign legacy DLManagedTensor as a Minarrow NdArray.
///
/// Only CPU float tensors matching the element type `T` are supported. The
/// NdArray takes ownership of the DLManagedTensor and will call its deleter
/// when dropped.
///
/// # Safety
/// - The DLManagedTensor must be valid and point to accessible CPU memory
/// - The caller transfers ownership - the tensor must not be used after this call
/// - The tensor must contain data of element type `T`
/// - The imported NdArray is Send + Sync, so the tensor's deleter must
///   tolerate running on any thread
pub unsafe fn import_from_dlpack<T: Float>(
    managed_ptr: *mut DLManagedTensor,
) -> Result<NdArray<T>, MinarrowError> {
    if managed_ptr.is_null() {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: "null DLManagedTensor pointer".to_string(),
        });
    }
    let foreign = ForeignDLPack {
        ptr: std::ptr::null(),
        len_bytes: 0,
        managed: ForeignManaged::Legacy(managed_ptr),
    };
    let tensor = unsafe { &(*managed_ptr).dl_tensor };
    unsafe { import_dl_tensor(tensor, foreign) }
}

/// Import a foreign DLPack 1.x versioned managed tensor as a Minarrow
/// NdArray.
///
/// Rejects tensors stamped with a major version newer than
/// [`DLPACK_MAJOR_VERSION`]. A tensor carrying the read-only flag lands
/// as a shared buffer, so the first mutation copies rather than writing
/// through to the producer's memory.
///
/// # Safety
/// - The DLManagedTensorVersioned must be valid and point to accessible CPU memory
/// - The caller transfers ownership - the tensor must not be used after this call
/// - The tensor must contain data of element type `T`
/// - The imported NdArray is Send + Sync, so the tensor's deleter must
///   tolerate running on any thread
pub unsafe fn import_from_dlpack_versioned<T: Float>(
    managed_ptr: *mut DLManagedTensorVersioned,
) -> Result<NdArray<T>, MinarrowError> {
    if managed_ptr.is_null() {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: "null DLManagedTensorVersioned pointer".to_string(),
        });
    }
    let foreign = ForeignDLPack {
        ptr: std::ptr::null(),
        len_bytes: 0,
        managed: ForeignManaged::Versioned(managed_ptr),
    };
    let version = unsafe { (*managed_ptr).version };
    if version.major > DLPACK_MAJOR_VERSION {
        return Err(MinarrowError::BridgeError {
            source: "dlpack",
            message: format!(
                "DLPack major version {} is newer than the supported {}",
                version.major, DLPACK_MAJOR_VERSION
            ),
        });
    }
    let tensor = unsafe { &(*managed_ptr).dl_tensor };
    unsafe { import_dl_tensor(tensor, foreign) }
}

// ****************************************************************
// Tests
// ****************************************************************

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(all(feature = "views", feature = "select"))]
    use crate::traits::selection::RowSelection;

    #[test]
    fn export_import_rank_zero_scalar() {
        let arr = NdArray::from_slice(&[42.0], &[]);
        let owned = export_to_dlpack(arr);
        let tensor = owned.tensor();
        assert_eq!(tensor.ndim, 0);
        assert!(tensor.shape.is_null());
        assert!(tensor.strides.is_null());
        assert_eq!(unsafe { *(tensor.data as *const f64) }, 42.0);

        let imported = unsafe { import_from_dlpack::<f64>(owned.into_raw()) }.unwrap();
        assert!(imported.shape().is_empty());
        assert!(imported.strides().is_empty());
        assert_eq!(imported.len(), 1);
        assert_eq!(imported.get(&[]), 42.0);

        let versioned = export_to_dlpack_versioned(NdArray::from_slice(&[7.0f32], &[]));
        assert_eq!(versioned.tensor().ndim, 0);
        assert!(versioned.tensor().shape.is_null());
        assert!(versioned.tensor().strides.is_null());
        let imported = unsafe {
            import_from_dlpack_versioned::<f32>(versioned.into_raw())
        }.unwrap();
        assert!(imported.shape().is_empty());
        assert_eq!(imported.get(&[]), 7.0);
    }

    #[test]
    fn export_roundtrip_1d() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0], &[3]);
        let owned = export_to_dlpack(arr);
        let tensor = owned.tensor();

        assert_eq!(tensor.ndim, 1);
        assert_eq!(tensor.dtype.code, 2);
        assert_eq!(tensor.dtype.bits, 64);
        assert_eq!(tensor.byte_offset, 0);

        let shape = unsafe { std::slice::from_raw_parts(tensor.shape, 1) };
        assert_eq!(shape[0], 3);

        let strides = unsafe { std::slice::from_raw_parts(tensor.strides, 1) };
        assert_eq!(strides[0], 1);

        let data = tensor.data as *const f64;
        assert_eq!(unsafe { *data }, 1.0);
        assert_eq!(unsafe { *data.add(1) }, 2.0);
        assert_eq!(unsafe { *data.add(2) }, 3.0);
        // owned drops here, calling deleter
    }

    #[test]
    fn export_2d_column_major_strides() {
        let arr = NdArray::from_slice(
            &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]
        );
        let owned = export_to_dlpack(arr);
        let tensor = owned.tensor();

        assert_eq!(tensor.ndim, 2);

        let shape = unsafe { std::slice::from_raw_parts(tensor.shape, 2) };
        assert_eq!(shape[0], 3);
        assert_eq!(shape[1], 2);

        let strides = unsafe { std::slice::from_raw_parts(tensor.strides, 2) };
        assert_eq!(strides[0], 1);
        assert_eq!(strides[1], 3);

        let data = tensor.data as *const f64;
        assert_eq!(unsafe { *data }, 1.0);
        assert_eq!(unsafe { *data.add(2) }, 3.0);
        assert_eq!(unsafe { *data.add(strides[1] as usize) }, 4.0);
        assert_eq!(unsafe { *data.add(strides[1] as usize + 2) }, 6.0);
    }

    #[test]
    fn export_preserves_data_lifetime() {
        let owned = {
            let arr = NdArray::from_slice(&[42.0, 99.0], &[2]);
            export_to_dlpack(arr)
        };
        let data = owned.tensor().data as *const f64;
        assert_eq!(unsafe { *data }, 42.0);
        assert_eq!(unsafe { *data.add(1) }, 99.0);
    }

    #[test]
    fn import_from_exported() {
        let original = NdArray::from_slice(
            &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]
        );
        let owned = export_to_dlpack(original);
        let raw = owned.into_raw();

        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.get(&[0, 0]), 1.0);
        assert_eq!(imported.get(&[2, 0]), 3.0);
        assert_eq!(imported.get(&[0, 1]), 4.0);
        assert_eq!(imported.get(&[2, 1]), 6.0);
    }

    #[test]
    fn import_null_ptr_fails() {
        let result = unsafe { import_from_dlpack::<f64>(std::ptr::null_mut()) };
        assert!(result.is_err());
    }

    #[test]
    fn export_3d_strides() {
        let data: Vec<f64> = (1..=24).map(|x| x as f64).collect();
        let arr = NdArray::from_slice(&data, &[2, 3, 4]);
        let owned = export_to_dlpack(arr);
        let tensor = owned.tensor();

        assert_eq!(tensor.ndim, 3);

        let shape = unsafe { std::slice::from_raw_parts(tensor.shape, 3) };
        assert_eq!(shape, &[2, 3, 4]);

        let strides = unsafe { std::slice::from_raw_parts(tensor.strides, 3) };
        assert_eq!(strides[0], 1);
        assert_eq!(strides[1], 2);
        assert_eq!(strides[2], 6);
    }

    #[test]
    fn to_dlpack_method() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let owned = arr.to_dlpack();
        let tensor = owned.tensor();
        assert_eq!(tensor.ndim, 2);
        let data = tensor.data as *const f64;
        assert_eq!(unsafe { *data }, 1.0);
    }

    #[test]
    fn export_roundtrip_f32() {
        let original = NdArray::<f32>::from_slice(
            &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]
        );
        let owned = export_to_dlpack(original);
        let raw = owned.into_raw();

        let imported = unsafe { import_from_dlpack::<f32>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.get(&[0, 0]), 1.0);
        assert_eq!(imported.get(&[2, 0]), 3.0);
        assert_eq!(imported.get(&[0, 1]), 4.0);
        assert_eq!(imported.get(&[2, 1]), 6.0);
    }

    #[test]
    fn export_f32_dtype_bits() {
        let arr = NdArray::<f32>::from_slice(&[1.0f32, 2.0, 3.0], &[3]);
        let owned = export_to_dlpack(arr);
        let tensor = owned.tensor();
        assert_eq!(tensor.dtype.code, 2);
        assert_eq!(tensor.dtype.bits, 32);
    }

    #[test]
    fn import_dtype_mismatch_fails() {
        // Export f64, then import as f32 - the bit width mismatch rejects.
        let f64_arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let raw = export_to_dlpack(f64_arr).into_raw();
        let result = unsafe { import_from_dlpack::<f32>(raw) };
        assert!(result.is_err());

        // Export f32, then import as f64 - likewise rejected.
        let f32_arr = NdArray::<f32>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2]);
        let raw = export_to_dlpack(f32_arr).into_raw();
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    // *** Versioned ABI ***********************************************

    #[test]
    fn versioned_roundtrip() {
        let original = NdArray::from_slice(
            &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]
        );
        let owned = export_to_dlpack_versioned(original);
        assert_eq!(owned.version().major, DLPACK_MAJOR_VERSION);
        assert_eq!(owned.version().minor, DLPACK_MINOR_VERSION);
        assert_eq!(owned.flags(), 0);

        let raw = owned.into_raw();
        let imported = unsafe { import_from_dlpack_versioned::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.get(&[0, 0]), 1.0);
        assert_eq!(imported.get(&[2, 1]), 6.0);
    }

    #[test]
    fn versioned_read_only_flag_on_shared_buffer() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0], &[3]);
        let clone = arr.clone();
        let owned = export_to_dlpack_versioned(clone);
        assert_ne!(owned.flags() & DLPACK_FLAG_BITMASK_READ_ONLY, 0);
        // The original still reads its data after the export drops.
        drop(owned);
        assert_eq!(arr.get(&[0]), 1.0);
    }

    #[test]
    fn versioned_rejects_newer_major() {
        let arr = NdArray::from_slice(&[1.0, 2.0], &[2]);
        let raw = export_to_dlpack_versioned(arr).into_raw();
        unsafe { (*raw).version.major = DLPACK_MAJOR_VERSION + 1 };
        let result = unsafe { import_from_dlpack_versioned::<f64>(raw) };
        assert!(result.is_err());
    }

    // *** View export *************************************************

    #[cfg(all(feature = "views", feature = "select"))]
    #[test]
    fn uniquely_owned_view_export_carries_byte_offset() {
        let (view, data_ptr) = {
            let data: Vec<f64> = (1..=10).map(|x| x as f64).collect();
            let arr = NdArray::from_slice(&data, &[5, 2]);
            let data_ptr = arr.as_slice().as_ptr();
            (arr.r(2..5), data_ptr)
        };
        let owned = export_view_to_dlpack(view);
        let tensor = owned.tensor();

        assert_eq!(tensor.byte_offset, 2 * std::mem::size_of::<f64>() as u64);
        assert_eq!(tensor.data as *const f64, data_ptr);
        let shape = unsafe { std::slice::from_raw_parts(tensor.shape, 2) };
        assert_eq!(shape, &[3, 2]);

        let raw = owned.into_raw();
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.get(&[0, 0]), 3.0);
        assert_eq!(imported.get(&[2, 1]), 10.0);
    }

    #[cfg(all(feature = "views", feature = "select"))]
    #[test]
    fn aliased_view_export_materialises() {
        let data: Vec<f64> = (1..=10).map(|x| x as f64).collect();
        let arr = NdArray::from_slice(&data, &[5, 2]);
        let owned = export_view_to_dlpack(arr.r(2..5));
        let tensor = owned.tensor();

        assert_eq!(tensor.byte_offset, 0);
        assert_ne!(tensor.data as *const f64, arr.as_slice().as_ptr());

        let raw = owned.into_raw();
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.get(&[0, 0]), arr.get(&[2, 0]));
        assert_eq!(imported.get(&[2, 1]), arr.get(&[4, 1]));
    }

    #[cfg(all(feature = "views", feature = "select"))]
    #[test]
    fn view_export_versioned_sets_read_only() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]);
        // The view shares the source buffer, so the export is read-only.
        let owned = export_view_to_dlpack_versioned(arr.r(0..2));
        assert_ne!(owned.flags() & DLPACK_FLAG_BITMASK_READ_ONLY, 0);
    }

    #[cfg(feature = "views")]
    #[test]
    fn transposed_view_export_roundtrip() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]);
        let raw = export_view_to_dlpack(arr.as_view().transpose()).into_raw();
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[2, 3]);
        assert_eq!(imported.get(&[0, 0]), arr.get(&[0, 0]));
        assert_eq!(imported.get(&[1, 2]), arr.get(&[2, 1]));
    }

    // *** Import validation *******************************************

    #[test]
    fn import_rejects_negative_shape() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let raw = export_to_dlpack(arr).into_raw();
        unsafe { *(*raw).dl_tensor.shape = -2 };
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    #[test]
    fn import_rejects_negative_strides() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let raw = export_to_dlpack(arr).into_raw();
        unsafe { *(*raw).dl_tensor.strides = -1 };
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    #[test]
    fn import_rejects_misaligned_byte_offset() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[4]);
        let raw = export_to_dlpack(arr).into_raw();
        unsafe { (*raw).dl_tensor.byte_offset = 4 };
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    #[test]
    fn import_rejects_overflowing_extents() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let raw = export_to_dlpack(arr).into_raw();
        unsafe {
            *(*raw).dl_tensor.shape = i64::MAX / 4;
            *(*raw).dl_tensor.strides = i64::MAX / 4;
        }
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    #[test]
    fn import_rejects_unknown_device() {
        let arr = NdArray::from_slice(&[1.0, 2.0], &[2]);
        let raw = export_to_dlpack(arr).into_raw();
        // kDLCUDAManaged, a code outside the host-memory set.
        unsafe { (*raw).dl_tensor.device.device_type = DLDeviceType::CUDA_MANAGED };
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        assert!(result.is_err());
    }

    #[test]
    fn import_null_strides_as_row_major() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2]);
        let raw = export_to_dlpack(arr).into_raw();
        unsafe { (*raw).dl_tensor.strides = std::ptr::null_mut() };
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.shape(), &[3, 2]);
        assert_eq!(imported.strides(), &[2, 1]);
        // Row-major reinterpretation of the buffer.
        assert_eq!(imported.get(&[0, 0]), 1.0);
        assert_eq!(imported.get(&[0, 1]), 2.0);
        assert_eq!(imported.get(&[1, 0]), 3.0);
    }

    // *** Foreign producer tensors ************************************

    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Shape and strides storage plus the counter the deleter bumps,
    /// keeping the foreign tensor's pointers alive until release.
    struct ForeignCtx {
        shape: Vec<i64>,
        strides: Vec<i64>,
        hits: &'static AtomicUsize,
    }

    unsafe extern "C" fn counting_deleter(managed: *mut DLManagedTensor) {
        if managed.is_null() {
            return;
        }
        let managed = unsafe { Box::from_raw(managed) };
        let ctx: Box<ForeignCtx> =
            unsafe { Box::from_raw(managed.manager_ctx as *mut ForeignCtx) };
        ctx.hits.fetch_add(1, Ordering::SeqCst);
    }

    /// Assemble a foreign-owned legacy managed tensor over caller-supplied
    /// f64 data, wired to the counting deleter.
    fn foreign_tensor(
        data: *mut c_void,
        shape: Vec<i64>,
        strides: Vec<i64>,
        hits: &'static AtomicUsize,
    ) -> *mut DLManagedTensor {
        let mut ctx = Box::new(ForeignCtx { shape, strides, hits });
        let tensor = DLTensor {
            data,
            device: DLDevice::cpu(),
            ndim: ctx.shape.len() as i32,
            dtype: DLDataType::FLOAT64,
            shape: ctx.shape.as_mut_ptr(),
            strides: ctx.strides.as_mut_ptr(),
            byte_offset: 0,
        };
        Box::into_raw(Box::new(DLManagedTensor {
            dl_tensor: tensor,
            manager_ctx: Box::into_raw(ctx) as *mut c_void,
            deleter: Some(counting_deleter),
        }))
    }

    /// Allocate a 64-byte aligned f64 buffer the test controls outright,
    /// so a zero-copy import shares it without a realignment copy.
    fn aligned_f64_alloc(values: &[f64]) -> (*mut f64, std::alloc::Layout) {
        let layout = std::alloc::Layout::from_size_align(
            values.len() * std::mem::size_of::<f64>(),
            64,
        )
        .unwrap();
        let ptr = unsafe { std::alloc::alloc(layout) as *mut f64 };
        assert!(!ptr.is_null());
        unsafe { std::ptr::copy_nonoverlapping(values.as_ptr(), ptr, values.len()) };
        (ptr, layout)
    }

    #[test]
    fn import_rejects_null_shape_pointer() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let raw = foreign_tensor(64 as *mut c_void, vec![2], vec![1], &HITS);
        unsafe { (*raw).dl_tensor.shape = std::ptr::null_mut() };
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        let Err(MinarrowError::BridgeError { message, .. }) = result else {
            panic!("expected BridgeError");
        };
        assert!(message.contains("null shape pointer"));
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn import_rejects_null_data_pointer() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let raw = foreign_tensor(std::ptr::null_mut(), vec![2], vec![1], &HITS);
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        let Err(MinarrowError::BridgeError { message, .. }) = result else {
            panic!("expected BridgeError");
        };
        assert!(message.contains("null data pointer"));
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn import_rejects_span_beyond_isize_max() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        // Validation rejects the span before any read, so the dangling
        // but aligned data pointer is never dereferenced.
        let raw = foreign_tensor(64 as *mut c_void, vec![i64::MAX / 4], vec![1], &HITS);
        let result = unsafe { import_from_dlpack::<f64>(raw) };
        let Err(MinarrowError::BridgeError { message, .. }) = result else {
            panic!("expected BridgeError");
        };
        assert!(message.contains("spans more than isize::MAX bytes"));
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn deleter_runs_once_on_drop() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let (data, layout) = aligned_f64_alloc(&[1.0, 2.0, 3.0, 4.0]);
        let raw = foreign_tensor(data as *mut c_void, vec![4], vec![1], &HITS);
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        assert_eq!(imported.get(&[2]), 3.0);
        assert_eq!(HITS.load(Ordering::SeqCst), 0);
        drop(imported);
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
        unsafe { std::alloc::dealloc(data as *mut u8, layout) };
    }

    #[test]
    fn deleter_runs_once_on_failed_import() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let (data, layout) = aligned_f64_alloc(&[1.0, 2.0]);
        let raw = foreign_tensor(data as *mut c_void, vec![2], vec![1], &HITS);
        // Importing as f32 against the f64 payload fails the dtype check.
        let result = unsafe { import_from_dlpack::<f32>(raw) };
        assert!(result.is_err());
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
        unsafe { std::alloc::dealloc(data as *mut u8, layout) };
    }

    #[test]
    fn import_mutation_copies_rather_than_writing_through() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let (data, layout) = aligned_f64_alloc(&[1.0, 2.0, 3.0, 4.0]);
        let raw = foreign_tensor(data as *mut c_void, vec![4], vec![1], &HITS);
        let mut imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        imported.set(&[1], 99.0);
        assert_eq!(imported.get(&[1]), 99.0);
        // The producer's memory holds its original values.
        let original = unsafe { std::slice::from_raw_parts(data, 4) };
        assert_eq!(original, &[1.0, 2.0, 3.0, 4.0]);
        drop(imported);
        unsafe { std::alloc::dealloc(data as *mut u8, layout) };
    }

    #[test]
    fn export_aliased_buffer_copies() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0], &[3]);
        let alias = arr.clone();
        let exported = export_to_dlpack(arr);
        assert_ne!(
            exported.tensor().data as *const f64,
            alias.as_slice().as_ptr()
        );
    }

    #[test]
    fn export_uniquely_owned_buffer_is_zero_copy() {
        let arr = NdArray::from_slice(&[1.0, 2.0, 3.0], &[3]);
        let data_ptr = arr.as_slice().as_ptr();
        let exported = export_to_dlpack(arr);
        assert_eq!(exported.tensor().data as *const f64, data_ptr);
    }

    #[test]
    fn imported_ndarray_send_drops_on_other_thread() {
        static HITS: AtomicUsize = AtomicUsize::new(0);
        let (data, layout) = aligned_f64_alloc(&[1.0, 2.0, 3.0, 4.0]);
        let raw = foreign_tensor(data as *mut c_void, vec![4], vec![1], &HITS);
        let imported = unsafe { import_from_dlpack::<f64>(raw) }.unwrap();
        std::thread::spawn(move || {
            assert_eq!(imported.get(&[3]), 4.0);
            drop(imported);
        })
        .join()
        .unwrap();
        assert_eq!(HITS.load(Ordering::SeqCst), 1);
        unsafe { std::alloc::dealloc(data as *mut u8, layout) };
    }
}