scirs2-core 0.4.2

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! # Memory-Mapped NDArray Wrapper
//!
//! This module provides a zero-copy, file-backed ndarray with Copy-on-Write (COW) semantics.
//!
//! ## Overview
//!
//! [`MmapArray<F>`] wraps a file on disk as an ndarray, enabling:
//!
//! - **Zero-copy reads**: Data is served directly from OS page cache without copying to RAM.
//! - **Read-write mmap**: Mutations are written directly to the file via mmap.
//! - **Copy-on-Write**: Reads are zero-copy; the first write triggers a copy of the data to RAM,
//!   after which all writes stay in RAM until explicitly persisted.
//!
//! ## File Format
//!
//! The binary file starts with a 64-byte header (little-endian):
//!
//! ```text
//! Offset  Size  Field
//! ------  ----  -----
//!  0..4    4    Magic bytes: b"MMAP"
//!  4       1    Version: 1
//!  5       1    dtype_id (1=f32, 2=f64, 3=i32, 4=i64)
//!  6..8    2    ndim (u16, little-endian)
//!  8..16   8    total_elements (u64, little-endian)
//! 16..16+8*ndim  8 per dim  shape dimensions (u64 each, little-endian)
//! ... zero-padding to byte 64
//! 64..    data  Raw element bytes (F, little-endian, row-major / C order)
//! ```
//!
//! ## Example
//!
//! ```rust,no_run
//! # #[cfg(feature = "mmap")]
//! # {
//! use scirs2_core::memory::mmap_array::{MmapArray, MmapError};
//! use ndarray::ArrayD;
//!
//! let tmp = std::env::temp_dir().join("example.mmap");
//! let data = ArrayD::<f32>::zeros(ndarray::IxDyn(&[4, 8]));
//! let arr = MmapArray::<f32>::create(&tmp, &data).expect("should succeed");
//! assert_eq!(arr.shape(), &[4, 8]);
//! # }
//! ```

use memmap2::{Mmap, MmapMut, MmapOptions};
use ndarray::{Array, ArrayView, ArrayViewMut, IxDyn};
use std::fs::OpenOptions;
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// Header constants
// ---------------------------------------------------------------------------

/// Magic bytes at the start of every mmap array file.
const MAGIC: &[u8; 4] = b"MMAP";
/// Current file format version.
const FORMAT_VERSION: u8 = 1;
/// Total header size in bytes. Data begins at this offset.
const HEADER_SIZE: usize = 64;

// ---------------------------------------------------------------------------
// MmapElement trait
// ---------------------------------------------------------------------------

/// Types that can be stored in a memory-mapped file.
///
/// Implementors must be:
/// - `Copy` — trivially duplicable, no heap ownership
/// - `bytemuck::Pod` — safe for byte-level reinterpretation
/// - `bytemuck::Zeroable` — a zero-initialized value is valid
/// - `'static` — no borrowed references
pub trait MmapElement: Copy + bytemuck::Pod + bytemuck::Zeroable + 'static {
    /// Unique byte tag written to the file header identifying this type.
    fn dtype_id() -> u8;
    /// Size of a single element in bytes.
    fn element_size() -> usize;
}

impl MmapElement for f32 {
    fn dtype_id() -> u8 {
        1
    }
    fn element_size() -> usize {
        4
    }
}

impl MmapElement for f64 {
    fn dtype_id() -> u8 {
        2
    }
    fn element_size() -> usize {
        8
    }
}

impl MmapElement for i32 {
    fn dtype_id() -> u8 {
        3
    }
    fn element_size() -> usize {
        4
    }
}

impl MmapElement for i64 {
    fn dtype_id() -> u8 {
        4
    }
    fn element_size() -> usize {
        8
    }
}

// ---------------------------------------------------------------------------
// Internal storage enum
// ---------------------------------------------------------------------------

/// The underlying memory storage — one of three modes.
enum MmapStorage<F: MmapElement> {
    /// Read-only mapping backed directly by the file.
    ReadOnly {
        mmap: Mmap,
        _phantom: std::marker::PhantomData<F>,
    },
    /// Read-write mapping backed directly by the file.
    ReadWrite {
        mmap: MmapMut,
        _phantom: std::marker::PhantomData<F>,
    },
    /// Copy-on-write: reads zero-copy from `mmap`; first write populates `cow_data`.
    CopyOnWrite {
        mmap: Mmap,
        /// `None` until the first write triggers the copy.
        cow_data: Option<Vec<F>>,
    },
}

// ---------------------------------------------------------------------------
// MmapArray
// ---------------------------------------------------------------------------

/// A zero-copy, file-backed ndarray with optional Copy-on-Write semantics.
///
/// The array is stored in a flat binary file with a 64-byte header.  Array
/// data is accessed via `memmap2`, so the OS manages paging automatically.
///
/// # Type Parameter
///
/// `F` must implement [`MmapElement`] (currently: `f32`, `f64`, `i32`, `i64`).
pub struct MmapArray<F: MmapElement> {
    storage: MmapStorage<F>,
    shape: Vec<usize>,
    /// C-order strides in element counts (not bytes).
    strides: Vec<usize>,
    file_path: PathBuf,
}

impl<F: MmapElement> std::fmt::Debug for MmapArray<F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mode = match &self.storage {
            MmapStorage::ReadOnly { .. } => "ReadOnly",
            MmapStorage::ReadWrite { .. } => "ReadWrite",
            MmapStorage::CopyOnWrite { cow_data, .. } => {
                if cow_data.is_some() {
                    "CopyOnWrite(dirty)"
                } else {
                    "CopyOnWrite(clean)"
                }
            }
        };
        f.debug_struct("MmapArray")
            .field("mode", &mode)
            .field("shape", &self.shape)
            .field("strides", &self.strides)
            .field("file_path", &self.file_path)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Header encode / decode
// ---------------------------------------------------------------------------

/// Encode a 64-byte header into `buf`.
///
/// # Panics
///
/// Panics if `ndim` > 6 (the header only has room for 6 shape dimensions).
fn encode_header(buf: &mut [u8; HEADER_SIZE], dtype_id: u8, shape: &[usize]) {
    let ndim = shape.len();
    assert!(
        ndim <= 6,
        "MmapArray supports at most 6 dimensions (header limit)"
    );

    buf.fill(0);

    // Magic
    buf[0..4].copy_from_slice(MAGIC);
    // Version
    buf[4] = FORMAT_VERSION;
    // dtype_id
    buf[5] = dtype_id;
    // ndim (u16 LE)
    let ndim_u16 = ndim as u16;
    buf[6..8].copy_from_slice(&ndim_u16.to_le_bytes());
    // total_elements (u64 LE)
    let total: u64 = shape.iter().product::<usize>() as u64;
    buf[8..16].copy_from_slice(&total.to_le_bytes());
    // shape dimensions
    for (i, &dim) in shape.iter().enumerate() {
        let off = 16 + i * 8;
        buf[off..off + 8].copy_from_slice(&(dim as u64).to_le_bytes());
    }
}

/// Decode a 64-byte header, returning `(dtype_id, shape)`.
fn decode_header(buf: &[u8; HEADER_SIZE]) -> Result<(u8, Vec<usize>), MmapError> {
    if &buf[0..4] != MAGIC {
        return Err(MmapError::InvalidMagic);
    }
    let version = buf[4];
    if version != FORMAT_VERSION {
        return Err(MmapError::VersionMismatch(version));
    }
    let dtype_id = buf[5];
    let ndim = u16::from_le_bytes([buf[6], buf[7]]) as usize;
    let _total_elements = u64::from_le_bytes(buf[8..16].try_into().map_err(|_| {
        MmapError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "header truncated at total_elements",
        ))
    })?);

    let mut shape = Vec::with_capacity(ndim);
    for i in 0..ndim {
        let off = 16 + i * 8;
        if off + 8 > HEADER_SIZE {
            return Err(MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("header too small for ndim={}", ndim),
            )));
        }
        let dim = u64::from_le_bytes(buf[off..off + 8].try_into().map_err(|_| {
            MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "header truncated in shape",
            ))
        })?);
        shape.push(dim as usize);
    }

    Ok((dtype_id, shape))
}

/// Compute C-order (row-major) strides for the given shape (element counts, not bytes).
fn c_strides(shape: &[usize]) -> Vec<usize> {
    let ndim = shape.len();
    let mut strides = vec![1usize; ndim];
    for i in (0..ndim.saturating_sub(1)).rev() {
        strides[i] = strides[i + 1] * shape[i + 1];
    }
    strides
}

/// Total element count for a shape slice.
fn total_elements(shape: &[usize]) -> usize {
    shape.iter().product()
}

// ---------------------------------------------------------------------------
// impl MmapArray
// ---------------------------------------------------------------------------

impl<F: MmapElement> MmapArray<F> {
    // -----------------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------------

    /// Create a new memory-mapped file from an existing ndarray and return a
    /// **read-write** [`MmapArray`] backed by that file.
    ///
    /// The input array must be in standard C-order (contiguous) layout.
    /// A 64-byte header is written first, followed immediately by the raw
    /// element bytes.
    ///
    /// # Errors
    ///
    /// Returns [`MmapError::NonContiguous`] if `data` is not a contiguous array.
    pub fn create(path: &Path, data: &Array<F, IxDyn>) -> Result<Self, MmapError> {
        // Require a contiguous layout so we can do a single slice copy.
        if !data.is_standard_layout() {
            return Err(MmapError::NonContiguous);
        }

        let shape = data.shape().to_vec();
        let n_elems = total_elements(&shape);
        let data_bytes = n_elems * F::element_size();
        let file_size = HEADER_SIZE + data_bytes;

        // Open / create file, set length.
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)?;
        file.set_len(file_size as u64)?;

        // Write header via a plain write (simpler than mmap for small header).
        {
            use std::io::BufWriter;
            let mut writer = BufWriter::new(&file);
            let mut header = [0u8; HEADER_SIZE];
            encode_header(&mut header, F::dtype_id(), &shape);
            writer.write_all(&header)?;
            // Write element bytes from the array's raw slice.
            let raw: &[F] = data.as_slice().ok_or(MmapError::NonContiguous)?;
            writer.write_all(bytemuck::cast_slice(raw))?;
            writer.flush()?;
        }

        // Now open a read-write mmap over the entire file (including header).
        // We use the whole file mapping and expose only the data region.
        let mmap = unsafe { MmapOptions::new().map_mut(&file)? };

        let strides = c_strides(&shape);
        Ok(Self {
            storage: MmapStorage::ReadWrite {
                mmap,
                _phantom: std::marker::PhantomData,
            },
            shape,
            strides,
            file_path: path.to_path_buf(),
        })
    }

    /// Open an existing `.mmap` file in **read-only** mode.
    ///
    /// The returned array cannot be mutated; calls to [`view_mut`](Self::view_mut)
    /// will return [`MmapError::ReadOnly`].
    pub fn open_read_only(path: &Path) -> Result<Self, MmapError> {
        let file = OpenOptions::new().read(true).open(path)?;
        let mmap = unsafe { MmapOptions::new().map(&file)? };
        Self::from_readonly_mmap(mmap, path)
    }

    /// Open an existing `.mmap` file in **read-write** mode.
    ///
    /// Mutations are written directly to the file (no buffering).
    pub fn open_read_write(path: &Path) -> Result<Self, MmapError> {
        let file = OpenOptions::new().read(true).write(true).open(path)?;
        let mmap = unsafe { MmapOptions::new().map_mut(&file)? };

        // Read and validate header from the mapping itself.
        if mmap.len() < HEADER_SIZE {
            return Err(MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "file too small to contain header",
            )));
        }
        let header_bytes: &[u8; HEADER_SIZE] = mmap[..HEADER_SIZE].try_into().map_err(|_| {
            MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "could not read header",
            ))
        })?;
        let (dtype_id, shape) = decode_header(header_bytes)?;
        if dtype_id != F::dtype_id() {
            return Err(MmapError::DtypeMismatch {
                expected: F::dtype_id(),
                actual: dtype_id,
            });
        }

        let strides = c_strides(&shape);
        Ok(Self {
            storage: MmapStorage::ReadWrite {
                mmap,
                _phantom: std::marker::PhantomData,
            },
            shape,
            strides,
            file_path: path.to_path_buf(),
        })
    }

    /// Open an existing `.mmap` file in **Copy-on-Write** mode.
    ///
    /// Reads are served zero-copy from the OS page cache.  The first call to
    /// [`view_mut`](Self::view_mut) triggers a full copy of the data into RAM,
    /// after which all mutations happen in-memory.  The original file is never
    /// modified unless you later call [`flush`](Self::flush) (which, in COW mode,
    /// is a no-op since there is no writable mapping to flush).
    pub fn open_cow(path: &Path) -> Result<Self, MmapError> {
        let file = OpenOptions::new().read(true).open(path)?;
        let mmap = unsafe { MmapOptions::new().map(&file)? };
        let (_, shape, strides) = Self::parse_readonly_mmap(&mmap, path)?;
        Ok(Self {
            storage: MmapStorage::CopyOnWrite {
                mmap,
                cow_data: None,
            },
            shape,
            strides,
            file_path: path.to_path_buf(),
        })
    }

    // -----------------------------------------------------------------------
    // Accessors
    // -----------------------------------------------------------------------

    /// Return a read-only ndarray view.
    ///
    /// For read-only and read-write modes, the view is zero-copy (backed by the mmap).
    /// For COW mode before any write, the view is also zero-copy.
    /// For COW mode after the first write, the view is backed by the in-RAM copy.
    pub fn view(&self) -> Result<ArrayView<'_, F, IxDyn>, MmapError> {
        match &self.storage {
            MmapStorage::ReadOnly { mmap, .. } => {
                let data_slice = &mmap[HEADER_SIZE..];
                let elems: &[F] = bytemuck::cast_slice(data_slice);
                let ix = IxDyn(self.shape.as_slice());
                // SAFETY: We own the mmap for the lifetime of `&self`, the
                // pointer is valid, and the length matches `self.shape`.
                let view = unsafe { ArrayView::from_shape_ptr(ix, elems.as_ptr()) };
                Ok(view)
            }
            MmapStorage::ReadWrite { mmap, .. } => {
                let data_slice = &mmap[HEADER_SIZE..];
                let elems: &[F] = bytemuck::cast_slice(data_slice);
                let ix = IxDyn(self.shape.as_slice());
                // SAFETY: Same as above.
                let view = unsafe { ArrayView::from_shape_ptr(ix, elems.as_ptr()) };
                Ok(view)
            }
            MmapStorage::CopyOnWrite { mmap, cow_data } => {
                match cow_data {
                    None => {
                        // No write yet — serve directly from the mmap.
                        let data_slice = &mmap[HEADER_SIZE..];
                        let elems: &[F] = bytemuck::cast_slice(data_slice);
                        let ix = IxDyn(self.shape.as_slice());
                        // SAFETY: Mmap is valid for `&self` lifetime.
                        let view = unsafe { ArrayView::from_shape_ptr(ix, elems.as_ptr()) };
                        Ok(view)
                    }
                    Some(data) => {
                        let ix = IxDyn(self.shape.as_slice());
                        // SAFETY: `data` is a Vec owned by `self`, pointer valid for `&self`.
                        let view = unsafe { ArrayView::from_shape_ptr(ix, data.as_ptr()) };
                        Ok(view)
                    }
                }
            }
        }
    }

    /// Return a mutable ndarray view.
    ///
    /// - **ReadOnly**: always returns [`MmapError::ReadOnly`].
    /// - **ReadWrite**: the view is zero-copy and writes go directly to the file.
    /// - **COW**: triggers a copy of the mmap data into RAM on the first call.
    pub fn view_mut(&mut self) -> Result<ArrayViewMut<'_, F, IxDyn>, MmapError> {
        match &mut self.storage {
            MmapStorage::ReadOnly { .. } => Err(MmapError::ReadOnly),
            MmapStorage::ReadWrite { mmap, .. } => {
                let data_slice = &mut mmap[HEADER_SIZE..];
                let elems: &mut [F] = bytemuck::cast_slice_mut(data_slice);
                let ix = IxDyn(self.shape.as_slice());
                // SAFETY: We have exclusive access via `&mut self`.
                let view = unsafe { ArrayViewMut::from_shape_ptr(ix, elems.as_mut_ptr()) };
                Ok(view)
            }
            MmapStorage::CopyOnWrite { mmap, cow_data } => {
                // Trigger COW fault if this is the first write.
                if cow_data.is_none() {
                    let data_slice = &mmap[HEADER_SIZE..];
                    let elems: &[F] = bytemuck::cast_slice(data_slice);
                    *cow_data = Some(elems.to_vec());
                }
                let data = cow_data.as_mut().ok_or_else(|| {
                    MmapError::Io(std::io::Error::other(
                        "COW data unexpectedly None after initialization",
                    ))
                })?;
                let ix = IxDyn(self.shape.as_slice());
                // SAFETY: We have exclusive access via `&mut self`.
                let view = unsafe { ArrayViewMut::from_shape_ptr(ix, data.as_mut_ptr()) };
                Ok(view)
            }
        }
    }

    /// Flush changes to disk.
    ///
    /// - **ReadWrite**: calls `MmapMut::flush()`.
    /// - **ReadOnly** / **COW**: no-op (returns `Ok(())`).
    pub fn flush(&self) -> Result<(), MmapError> {
        match &self.storage {
            MmapStorage::ReadWrite { mmap, .. } => {
                mmap.flush()?;
                Ok(())
            }
            _ => Ok(()),
        }
    }

    /// Return the shape of the array.
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }

    /// Return the C-order strides (in element counts).
    pub fn strides(&self) -> &[usize] {
        &self.strides
    }

    /// Return the total number of elements.
    pub fn len(&self) -> usize {
        total_elements(&self.shape)
    }

    /// Return `true` if the array has zero elements.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return the file path backing this array.
    pub fn file_path(&self) -> &Path {
        &self.file_path
    }

    /// Copy all elements into a heap-allocated [`Array<F, IxDyn>`].
    ///
    /// This always copies; it is equivalent to `.view()?.to_owned()`.
    pub fn to_owned_array(&self) -> Result<Array<F, IxDyn>, MmapError> {
        let view = self.view()?;
        Ok(view.to_owned())
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Construct a `ReadOnly` variant from an already-mapped region.
    fn from_readonly_mmap(mmap: Mmap, path: &Path) -> Result<Self, MmapError> {
        let (_, shape, strides) = Self::parse_readonly_mmap(&mmap, path)?;
        Ok(Self {
            storage: MmapStorage::ReadOnly {
                mmap,
                _phantom: std::marker::PhantomData,
            },
            shape,
            strides,
            file_path: path.to_path_buf(),
        })
    }

    /// Validate and parse the header from a read-only mapping.
    ///
    /// Returns `(dtype_id, shape, strides)`.
    fn parse_readonly_mmap(
        mmap: &Mmap,
        _path: &Path,
    ) -> Result<(u8, Vec<usize>, Vec<usize>), MmapError> {
        if mmap.len() < HEADER_SIZE {
            return Err(MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "file too small to contain header",
            )));
        }
        let header_bytes: &[u8; HEADER_SIZE] = mmap[..HEADER_SIZE].try_into().map_err(|_| {
            MmapError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "could not read header slice",
            ))
        })?;
        let (dtype_id, shape) = decode_header(header_bytes)?;
        if dtype_id != F::dtype_id() {
            return Err(MmapError::DtypeMismatch {
                expected: F::dtype_id(),
                actual: dtype_id,
            });
        }
        let strides = c_strides(&shape);
        Ok((dtype_id, shape, strides))
    }

    // -----------------------------------------------------------------------
    // Test-only helpers
    // -----------------------------------------------------------------------

    /// Create an anonymous (non-file-backed) mmap for testing purposes.
    ///
    /// This uses an anonymous mapping backed by a temporary file.
    #[cfg(test)]
    fn create_anonymous(shape: &[usize]) -> Result<Self, MmapError> {
        use tempfile::tempfile;

        let n_elems = total_elements(shape);
        let file_size = HEADER_SIZE + n_elems * F::element_size();

        let file = tempfile()?;
        file.set_len(file_size as u64)?;

        // Write header.
        {
            use std::io::BufWriter;
            let mut writer = BufWriter::new(&file);
            let mut header = [0u8; HEADER_SIZE];
            encode_header(&mut header, F::dtype_id(), shape);
            writer.write_all(&header)?;
            // Data is zero-initialized by `set_len`.
            writer.flush()?;
        }

        let mmap = unsafe { MmapOptions::new().map_mut(&file)? };
        let strides = c_strides(shape);

        // We store a dummy path since this is anonymous.
        Ok(Self {
            storage: MmapStorage::ReadWrite {
                mmap,
                _phantom: std::marker::PhantomData,
            },
            shape: shape.to_vec(),
            strides,
            file_path: PathBuf::from("<anonymous>"),
        })
    }
}

// ---------------------------------------------------------------------------
// MmapError
// ---------------------------------------------------------------------------

/// Errors that can occur when creating or accessing a [`MmapArray`].
#[derive(Debug, thiserror::Error)]
pub enum MmapError {
    /// An I/O error occurred (file open, read, write, flush, etc.).
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// The file does not start with the expected magic bytes `b"MMAP"`.
    #[error("Invalid magic bytes — not a valid mmap array file")]
    InvalidMagic,

    /// The file header reports a format version other than 1.
    #[error("Version mismatch: expected 1, got {0}")]
    VersionMismatch(u8),

    /// The dtype tag in the file does not match the requested element type.
    #[error("Dtype mismatch: expected dtype_id {expected}, got {actual}")]
    DtypeMismatch { expected: u8, actual: u8 },

    /// The stored shape does not match the shape provided by the caller.
    #[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
    ShapeMismatch {
        expected: Vec<usize>,
        actual: Vec<usize>,
    },

    /// The source ndarray is not in contiguous C-order layout.
    #[error("Array is not contiguous (non-contiguous layouts not supported)")]
    NonContiguous,

    /// The mapping is read-only and a mutable view was requested.
    #[error("Array is read-only")]
    ReadOnly,
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::{ArrayD, IxDyn};

    /// Build a small f32 array filled with ascending values.
    fn make_f32_array(shape: &[usize]) -> ArrayD<f32> {
        let n = shape.iter().product::<usize>();
        let data: Vec<f32> = (0..n).map(|i| i as f32).collect();
        ArrayD::from_shape_vec(IxDyn(shape), data).expect("shape mismatch in test helper")
    }

    /// Build a small f64 array filled with ascending values.
    fn make_f64_array(shape: &[usize]) -> ArrayD<f64> {
        let n = shape.iter().product::<usize>();
        let data: Vec<f64> = (0..n).map(|i| i as f64).collect();
        ArrayD::from_shape_vec(IxDyn(shape), data).expect("shape mismatch in test helper")
    }

    // -----------------------------------------------------------------------
    // Header round-trip tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_header_round_trip_1d() {
        let mut buf = [0u8; HEADER_SIZE];
        encode_header(&mut buf, 1, &[100]);
        let (dtype_id, shape) = decode_header(&buf).expect("decode failed");
        assert_eq!(dtype_id, 1);
        assert_eq!(shape, vec![100usize]);
    }

    #[test]
    fn test_header_round_trip_2d() {
        let mut buf = [0u8; HEADER_SIZE];
        encode_header(&mut buf, 2, &[3, 4]);
        let (dtype_id, shape) = decode_header(&buf).expect("decode failed");
        assert_eq!(dtype_id, 2);
        assert_eq!(shape, vec![3, 4]);
    }

    #[test]
    fn test_header_round_trip_6d() {
        let mut buf = [0u8; HEADER_SIZE];
        encode_header(&mut buf, 4, &[2, 3, 4, 5, 6, 7]);
        let (dtype_id, shape) = decode_header(&buf).expect("decode failed");
        assert_eq!(dtype_id, 4);
        assert_eq!(shape, vec![2, 3, 4, 5, 6, 7]);
    }

    #[test]
    fn test_bad_magic() {
        let mut buf = [0u8; HEADER_SIZE];
        buf[0..4].copy_from_slice(b"NOPE");
        let err = decode_header(&buf).expect_err("should fail");
        assert!(matches!(err, MmapError::InvalidMagic));
    }

    #[test]
    fn test_bad_version() {
        let mut buf = [0u8; HEADER_SIZE];
        encode_header(&mut buf, 1, &[10]);
        buf[4] = 99; // corrupt version
        let err = decode_header(&buf).expect_err("should fail");
        assert!(matches!(err, MmapError::VersionMismatch(99)));
    }

    // -----------------------------------------------------------------------
    // c_strides
    // -----------------------------------------------------------------------

    #[test]
    fn test_c_strides_1d() {
        assert_eq!(c_strides(&[5]), vec![1]);
    }

    #[test]
    fn test_c_strides_2d() {
        assert_eq!(c_strides(&[3, 4]), vec![4, 1]);
    }

    #[test]
    fn test_c_strides_3d() {
        assert_eq!(c_strides(&[2, 3, 4]), vec![12, 4, 1]);
    }

    // -----------------------------------------------------------------------
    // MmapElement implementations
    // -----------------------------------------------------------------------

    #[test]
    fn test_dtype_ids_are_distinct() {
        let ids = [
            f32::dtype_id(),
            f64::dtype_id(),
            i32::dtype_id(),
            i64::dtype_id(),
        ];
        for i in 0..ids.len() {
            for j in (i + 1)..ids.len() {
                assert_ne!(ids[i], ids[j], "dtype IDs must be unique");
            }
        }
    }

    #[test]
    fn test_element_sizes() {
        assert_eq!(f32::element_size(), 4);
        assert_eq!(f64::element_size(), 8);
        assert_eq!(i32::element_size(), 4);
        assert_eq!(i64::element_size(), 8);
    }

    // -----------------------------------------------------------------------
    // create + open_read_only
    // -----------------------------------------------------------------------

    #[test]
    fn test_create_and_read_only_f32() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_create_ro_f32.mmap");

        let original = make_f32_array(&[4, 5]);
        {
            let arr = MmapArray::<f32>::create(&path, &original).expect("create failed");
            assert_eq!(arr.shape(), &[4, 5]);
            assert_eq!(arr.len(), 20);
            assert!(!arr.is_empty());
        }

        {
            let arr = MmapArray::<f32>::open_read_only(&path).expect("open_read_only failed");
            assert_eq!(arr.shape(), &[4, 5]);
            let view = arr.view().expect("view failed");
            // Check that values match
            for (a, b) in view.iter().zip(original.iter()) {
                assert!((a - b).abs() < f32::EPSILON, "element mismatch: {a} vs {b}");
            }
        }

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_create_and_read_only_f64() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_create_ro_f64.mmap");

        let original = make_f64_array(&[3, 3]);
        {
            let _arr = MmapArray::<f64>::create(&path, &original).expect("create failed");
        }

        let arr = MmapArray::<f64>::open_read_only(&path).expect("open_read_only failed");
        let owned = arr.to_owned_array().expect("to_owned failed");
        assert_eq!(owned.shape(), &[3, 3]);
        for (a, b) in owned.iter().zip(original.iter()) {
            assert!((a - b).abs() < f64::EPSILON);
        }

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_create_and_read_only_i32() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_create_ro_i32.mmap");

        let n = 6usize;
        let data: Vec<i32> = (0..n as i32).collect();
        let original = ArrayD::from_shape_vec(IxDyn(&[2, 3]), data).expect("shape mismatch");
        {
            let _arr = MmapArray::<i32>::create(&path, &original).expect("create failed");
        }

        let arr = MmapArray::<i32>::open_read_only(&path).expect("open_read_only failed");
        let view = arr.view().expect("view failed");
        for (a, b) in view.iter().zip(original.iter()) {
            assert_eq!(a, b);
        }

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // read-write mode
    // -----------------------------------------------------------------------

    #[test]
    fn test_read_write_mutation() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_rw.mmap");

        let original = make_f64_array(&[5]);
        {
            let mut arr = MmapArray::<f64>::create(&path, &original).expect("create failed");
            {
                let mut view = arr.view_mut().expect("view_mut failed");
                // Double every element in-place.
                view.iter_mut().for_each(|x| *x *= 2.0);
            }
            arr.flush().expect("flush failed");
        }

        // Re-open and verify changes persisted.
        let arr = MmapArray::<f64>::open_read_only(&path).expect("open_read_only failed");
        let view = arr.view().expect("view failed");
        for (i, &val) in view.iter().enumerate() {
            let expected = (i as f64) * 2.0;
            assert!(
                (val - expected).abs() < f64::EPSILON,
                "element {i}: got {val}, expected {expected}"
            );
        }

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_open_read_write_then_mutate() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_open_rw.mmap");

        let original = make_f32_array(&[3, 3]);
        {
            let _arr = MmapArray::<f32>::create(&path, &original).expect("create failed");
        }

        {
            let mut arr = MmapArray::<f32>::open_read_write(&path).expect("open_read_write failed");
            {
                let mut view = arr.view_mut().expect("view_mut failed");
                view.iter_mut().for_each(|x| *x += 100.0);
            }
            arr.flush().expect("flush failed");
        }

        let arr = MmapArray::<f32>::open_read_only(&path).expect("open_read_only failed");
        let view = arr.view().expect("view failed");
        for (i, &val) in view.iter().enumerate() {
            let expected = i as f32 + 100.0;
            assert!(
                (val - expected).abs() < f32::EPSILON,
                "element {i}: got {val}, expected {expected}"
            );
        }

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // read-only rejects mutation
    // -----------------------------------------------------------------------

    #[test]
    fn test_read_only_rejects_view_mut() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_ro_no_mut.mmap");

        let original = make_f32_array(&[2, 2]);
        {
            let _arr = MmapArray::<f32>::create(&path, &original).expect("create failed");
        }

        let mut arr = MmapArray::<f32>::open_read_only(&path).expect("open_read_only failed");
        let err = arr.view_mut().expect_err("should return ReadOnly error");
        assert!(matches!(err, MmapError::ReadOnly));

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // Copy-on-Write semantics
    // -----------------------------------------------------------------------

    #[test]
    fn test_cow_no_copy_before_write() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_cow_read.mmap");

        let original = make_f64_array(&[4]);
        {
            let _arr = MmapArray::<f64>::create(&path, &original).expect("create failed");
        }

        let arr = MmapArray::<f64>::open_cow(&path).expect("open_cow failed");
        // Before any write, cow_data should be None (verified by reading successfully).
        let view = arr.view().expect("view failed");
        for (a, b) in view.iter().zip(original.iter()) {
            assert!((a - b).abs() < f64::EPSILON);
        }

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_cow_mutates_in_ram_not_file() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_cow_mutate.mmap");

        let original = make_f32_array(&[6]);
        {
            let _arr = MmapArray::<f32>::create(&path, &original).expect("create failed");
        }

        {
            let mut arr = MmapArray::<f32>::open_cow(&path).expect("open_cow failed");
            {
                let mut view = arr.view_mut().expect("view_mut failed");
                // Write should trigger COW fault and copy to RAM.
                view.iter_mut().for_each(|x| *x = -1.0);
            }

            // In-memory view should reflect mutation.
            let view = arr.view().expect("view failed");
            for &val in view.iter() {
                assert!(
                    (val - (-1.0f32)).abs() < f32::EPSILON,
                    "COW in-memory data wrong: {val}"
                );
            }
            // flush is a no-op in COW mode; should not error.
            arr.flush().expect("flush failed");
        }

        // The original file must be UNCHANGED because we used COW.
        let arr_check = MmapArray::<f32>::open_read_only(&path).expect("open_read_only failed");
        let view_check = arr_check.view().expect("view failed");
        for (i, &val) in view_check.iter().enumerate() {
            let expected = i as f32;
            assert!(
                (val - expected).abs() < f32::EPSILON,
                "file was modified when COW was used: element {i}: got {val}"
            );
        }

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // dtype mismatch detection
    // -----------------------------------------------------------------------

    #[test]
    fn test_dtype_mismatch_on_open() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_dtype_mismatch.mmap");

        let original = make_f32_array(&[8]);
        {
            let _arr = MmapArray::<f32>::create(&path, &original).expect("create failed");
        }

        // Try to open as f64 — must fail with DtypeMismatch.
        let err = MmapArray::<f64>::open_read_only(&path).expect_err("should be DtypeMismatch");
        assert!(
            matches!(
                err,
                MmapError::DtypeMismatch {
                    expected: 2,
                    actual: 1
                }
            ),
            "unexpected error: {err:?}"
        );

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // Non-contiguous array rejection
    // -----------------------------------------------------------------------

    #[test]
    fn test_noncontiguous_rejected() {
        use ndarray::ShapeBuilder;

        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_noncontiguous.mmap");

        // Create a Fortran-order (column-major) array, which is NOT in standard
        // C (row-major) layout.  `create()` must reject it with NonContiguous.
        let fortran: ArrayD<f64> = ndarray::Array::zeros(IxDyn(&[3, 4]).f());
        assert!(
            !fortran.is_standard_layout(),
            "test precondition: Fortran array must be non-standard-layout"
        );

        let err = MmapArray::<f64>::create(&path, &fortran)
            .expect_err("create() should reject Fortran-order array");
        assert!(
            matches!(err, MmapError::NonContiguous),
            "expected NonContiguous, got: {err:?}"
        );

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // Anonymous mmap (test-only helper)
    // -----------------------------------------------------------------------

    #[test]
    fn test_anonymous_create() {
        let arr = MmapArray::<f32>::create_anonymous(&[8, 8]).expect("anonymous create failed");
        assert_eq!(arr.shape(), &[8, 8]);
        assert_eq!(arr.len(), 64);
        let view = arr.view().expect("view failed");
        for &val in view.iter() {
            assert_eq!(val, 0.0f32);
        }
    }

    #[test]
    fn test_anonymous_mutation() {
        let mut arr = MmapArray::<i64>::create_anonymous(&[4]).expect("anonymous create failed");
        {
            let mut view = arr.view_mut().expect("view_mut failed");
            for (i, x) in view.iter_mut().enumerate() {
                *x = i as i64 * 10;
            }
        }
        let view = arr.view().expect("view failed");
        for (i, &val) in view.iter().enumerate() {
            assert_eq!(val, i as i64 * 10, "element {i} wrong");
        }
    }

    // -----------------------------------------------------------------------
    // to_owned_array
    // -----------------------------------------------------------------------

    #[test]
    fn test_to_owned_array() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_to_owned.mmap");

        let original = make_f64_array(&[2, 3, 4]);
        {
            let _arr = MmapArray::<f64>::create(&path, &original).expect("create failed");
        }

        let arr = MmapArray::<f64>::open_read_only(&path).expect("open failed");
        let owned = arr.to_owned_array().expect("to_owned failed");
        assert_eq!(owned.shape(), original.shape());
        for (a, b) in owned.iter().zip(original.iter()) {
            assert!((a - b).abs() < f64::EPSILON);
        }

        let _ = std::fs::remove_file(&path);
    }

    // -----------------------------------------------------------------------
    // is_empty
    // -----------------------------------------------------------------------

    #[test]
    fn test_empty_array() {
        let dir = std::env::temp_dir();
        let path = dir.join("test_mmap_empty.mmap");

        // A 0-element array (shape [0]).
        let empty: ArrayD<f32> = ArrayD::zeros(IxDyn(&[0]));
        let arr = MmapArray::<f32>::create(&path, &empty).expect("create failed");
        assert!(arr.is_empty());
        assert_eq!(arr.len(), 0);
        assert_eq!(arr.shape(), &[0]);

        let _ = std::fs::remove_file(&path);
    }
}