qdrant-edge 0.7.1

A lightweight, in-process vector search engine designed for embedded devices, autonomous systems, and mobile agents.
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
//! Typed memory maps
//!
//! This module adds type to directly map types and a slice of types onto a memory mapped file.
//! The typed memory maps can be directly used as if it were that type.
//!
//! Types:
//! - [`MmapType`]
//! - [`MmapSlice`]
//! - [`MmapBitSlice`]
//!
//! Various additional functions are added for use within Qdrant, such as `flusher` to obtain a
//! flusher handle to explicitly flush the underlying memory map at a later time.
//!
//! # Safety
//!
//! Code in this module is `unsafe` and very error prone. It is therefore compacted in this single
//! module to make it easier to review, to make it easier to check for soundness, and to make it
//! easier to reason about. The interface provided by types in this module is as-safe-as-possible
//! and uses `unsafe` where appropriate.
//!
//! Please prevent touching code in this file. If modifications must be done, please do so with the
//! utmost care. Security is critical here as this is an easy place to introduce undefined
//! behavior. Problems caused by this are very hard to debug.

use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::sync::Arc;
use std::{fmt, mem, slice};

use memmap2::MmapMut;

use super::advice::{Advice, AdviceSetting, Madviseable};
use super::ops;
use crate::common::bitvec::BitSlice;

/// Result for mmap errors.
type Result<T> = std::result::Result<T, Error>;

pub type MmapFlusher = Box<dyn FnOnce() -> Result<()> + Send>;

/// Type `T` on a memory mapped file
///
/// Functions as if it is `T` because this implements [`Deref`] and [`DerefMut`].
///
/// # Safety
///
/// This directly maps (transmutes) the type onto the memory mapped data. This is dangerous and
/// very error prone and must be used with utmost care. Types holding references are not supported
/// for example. Malformed data in the mmap will break type `T` and will cause undefined behavior.
pub struct MmapType<T>
where
    T: ?Sized + 'static,
{
    /// Type accessor: mutable reference to access the type
    ///
    /// This has the same lifetime as the backing `mmap`, and thus this struct. A borrow must
    /// never be leased out for longer.
    ///
    /// Since we own this reference inside this struct, we can guarantee we never lease it out for
    /// longer.
    ///
    /// # Safety
    ///
    /// This is an alias to the data inside `mmap`. We should prevent using both together at all
    /// costs because the Rust compiler assumes `noalias` for optimization.
    ///
    /// See: <https://doc.rust-lang.org/nomicon/aliasing.html>
    r#type: &'static mut T,
    /// Type storage: memory mapped file as backing store for type
    ///
    /// Has an exact size to fit the type.
    ///
    /// This should never be accessed directly, because it shares a mutable reference with
    /// `r#type`. That must be used instead. The sole purpose of this is to keep ownership of the
    /// mmap, and to allow properly cleaning up when this struct is dropped.
    mmap: Arc<MmapMut>,
}

impl<T: ?Sized> fmt::Debug for MmapType<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MmapType")
            .field("mmap", &self.mmap)
            .finish_non_exhaustive()
    }
}

impl<T> MmapType<T>
where
    T: Sized + 'static,
{
    /// Transform a mmap into a typed mmap of type `T`.
    ///
    /// # Safety
    ///
    /// Unsafe because malformed data in the mmap may break type `T` resulting in undefined
    /// behavior.
    ///
    /// # Panics
    ///
    /// - panics when the size of the mmap doesn't match size `T`
    /// - panics when the mmap data is not correctly aligned for type `T`
    /// - See: [`mmap_prefix_to_type_unbounded`]
    pub unsafe fn from(mmap_with_type: MmapMut) -> Self {
        unsafe { Self::try_from(mmap_with_type).unwrap() }
    }

    /// Transform a mmap into a typed mmap of type `T`.
    ///
    /// Returns an error when the mmap has an incorrect size.
    ///
    /// # Safety
    ///
    /// Unsafe because malformed data in the mmap may break type `T` resulting in undefined
    /// behavior.
    ///
    /// # Panics
    ///
    /// - panics when the mmap data is not correctly aligned for type `T`
    /// - See: [`mmap_prefix_to_type_unbounded`]
    pub unsafe fn try_from(mut mmap_with_type: MmapMut) -> Result<Self> {
        let r#type = unsafe { mmap_prefix_to_type_unbounded(&mut mmap_with_type)? };
        let mmap = Arc::new(mmap_with_type);
        Ok(Self { r#type, mmap })
    }
}

impl<T> MmapType<[T]>
where
    T: 'static,
{
    /// Transform a mmap into a typed slice mmap of type `&[T]`.
    ///
    /// Returns an error when the mmap has an incorrect size.
    ///
    /// # Warning
    ///
    /// This does not support slices, because those cannot be transmuted directly because it has
    /// extra parts. See [`MmapSlice`] and [`std::slice::from_raw_parts`].
    ///
    /// # Safety
    ///
    /// Unsafe because malformed data in the mmap may break type `T` resulting in undefined
    /// behavior.
    ///
    /// # Panics
    ///
    /// - panics when the mmap data is not correctly aligned for type `T`
    /// - See: [`mmap_to_slice_unbounded`]
    pub unsafe fn try_slice_from(mut mmap_with_slice: MmapMut) -> Result<Self> {
        let r#type = unsafe { mmap_to_slice_unbounded(&mut mmap_with_slice, 0)? };
        let mmap = Arc::new(mmap_with_slice);
        Ok(Self { r#type, mmap })
    }
}

impl<T> MmapType<T>
where
    T: ?Sized + 'static,
{
    /// Get flusher to explicitly flush mmap at a later time
    pub fn flusher(&self) -> MmapFlusher {
        // TODO: if we explicitly flush when dropping this type, we can switch to a weak reference
        // here to only flush if it hasn't been done already
        Box::new({
            let mmap = self.mmap.clone();
            move || {
                // flushing a zero-sized mmap can cause panicking on some systems
                if !mmap.is_empty() {
                    mmap.flush()?;
                }
                Ok(())
            }
        })
    }

    /// Call [`memmap2::MmapMut::unchecked_advise`] on the underlying mmap.
    ///
    /// # Safety
    ///
    /// See [`memmap2::UncheckedAdvice`] doc.
    #[cfg(unix)]
    pub unsafe fn unchecked_advise(&self, advice: memmap2::UncheckedAdvice) -> std::io::Result<()> {
        unsafe { self.mmap.unchecked_advise(advice) }
    }

    pub fn populate(&self) -> std::io::Result<()> {
        self.mmap.populate();
        Ok(())
    }

    /// Hint to the OS that pages backing this mmap can be reclaimed.
    pub fn clear_cache(&self) -> std::io::Result<()> {
        let Self { r#type: _, mmap } = self;
        mmap.clear_cache();
        Ok(())
    }
}

impl<T> Deref for MmapType<T>
where
    T: ?Sized + 'static,
{
    type Target = T;

    // Has explicit 'bounded lifetime to clarify the inner reference never outlives this struct,
    // even though the reference has a static lifetime internally.
    #[allow(clippy::needless_lifetimes)]
    fn deref<'bounded>(&'bounded self) -> &'bounded Self::Target {
        self.r#type
    }
}

impl<T> DerefMut for MmapType<T>
where
    T: ?Sized + 'static,
{
    // Has explicit 'bounded lifetime to clarify the inner reference never outlives this struct,
    // even though the reference has a static lifetime internally.
    #[allow(clippy::needless_lifetimes)]
    fn deref_mut<'bounded>(&'bounded mut self) -> &'bounded mut Self::Target {
        self.r#type
    }
}

/// Slice of type `T` on a memory mapped file
///
/// Functions as if it is `&[T]` because this implements [`Deref`] and [`DerefMut`].
///
/// A helper because [`MmapType`] doesn't support slices directly.
pub struct MmapSlice<T>
where
    T: Sized + 'static,
{
    mmap: MmapType<[T]>,
}

impl<T> fmt::Debug for MmapSlice<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MmapSlice")
            .field("mmap", &self.mmap)
            .finish_non_exhaustive()
    }
}

impl<T> MmapSlice<T> {
    /// Transform a mmap into a typed slice mmap of type `&[T]`.
    ///
    /// This method is specifically intended for slices.
    ///
    /// # Safety
    ///
    /// Unsafe because malformed data in the mmap may break type `T` resulting in undefined
    /// behavior.
    ///
    /// # Panics
    ///
    /// - panics when the size of the mmap isn't a multiple of size `T`
    /// - panics when the mmap data is not correctly aligned for type `T`
    /// - See: [`mmap_to_slice_unbounded`]
    pub unsafe fn from(mmap_with_slice: MmapMut) -> Self {
        unsafe { Self::try_from(mmap_with_slice).unwrap() }
    }

    /// Transform a mmap into a typed slice mmap of type `&[T]`.
    ///
    /// This method is specifically intended for slices.
    ///
    /// Returns an error when the mmap has an incorrect size.
    ///
    /// # Safety
    ///
    /// Unsafe because malformed data in the mmap may break type `T` resulting in undefined
    /// behavior.
    ///
    /// # Panics
    ///
    /// - panics when the mmap data is not correctly aligned for type `T`
    /// - See: [`mmap_to_slice_unbounded`]
    pub unsafe fn try_from(mmap_with_slice: MmapMut) -> Result<Self> {
        let r#type = unsafe { MmapType::try_slice_from(mmap_with_slice) };
        r#type.map(|mmap| Self { mmap })
    }

    /// Get flusher to explicitly flush mmap at a later time
    pub fn flusher(&self) -> MmapFlusher {
        self.mmap.flusher()
    }

    pub fn create(path: &Path, mut iter: impl ExactSizeIterator<Item = T>) -> Result<()> {
        let file_len = iter.len() * mem::size_of::<T>();

        let _file = ops::create_and_ensure_length(path, file_len)?;

        let mmap = ops::open_write_mmap(
            path,
            AdviceSetting::Advice(Advice::Normal), // We only write sequentially
            false,
        )?;

        let mut mmap_slice = unsafe { Self::try_from(mmap)? };

        mmap_slice.fill_with(|| iter.next().expect("iterator size mismatch"));

        mmap_slice.flusher()()?;

        Ok(())
    }

    /// Populate all pages in the mmap.
    /// Block until all pages are populated.
    pub fn populate(&self) -> std::io::Result<()> {
        self.mmap.populate()?;
        Ok(())
    }

    /// Hint to the OS that pages backing this mmap can be reclaimed.
    pub fn clear_cache(&self) -> std::io::Result<()> {
        let Self { mmap } = self;
        mmap.clear_cache()?;
        Ok(())
    }
}

impl<T> Deref for MmapSlice<T> {
    type Target = MmapType<[T]>;

    fn deref(&self) -> &Self::Target {
        &self.mmap
    }
}

impl<T> DerefMut for MmapSlice<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.mmap
    }
}

impl<T: 'static> AsRef<[T]> for MmapSlice<T> {
    fn as_ref(&self) -> &[T] {
        &self.mmap
    }
}

/// [`BitSlice`] on a memory mapped file
///
/// Functions as if it is a [`BitSlice`] because this implements [`Deref`] and [`DerefMut`].
#[derive(Debug)]
pub struct MmapBitSlice {
    mmap: MmapType<BitSlice>,
}

impl MmapBitSlice {
    /// Minimum file size for the mmap file, in bytes.
    const MIN_FILE_SIZE: usize = mem::size_of::<usize>();

    /// Transform a mmap into a [`BitSlice`].
    ///
    /// A (non-zero) header size in bytes may be provided to omit from the BitSlice data.
    ///
    /// # Panics
    ///
    /// - panics when the size of the mmap isn't a multiple of the inner [`BitSlice`] type
    /// - panics when the mmap data is not correctly aligned to the inner [`BitSlice`] type
    /// - panics when the header size isn't a multiple of the inner [`BitSlice`] type
    /// - See: [`mmap_to_slice_unbounded`]
    pub fn from(mmap: MmapMut, header_size: usize) -> Self {
        Self::try_from(mmap, header_size).unwrap()
    }

    /// Transform a mmap into a [`BitSlice`].
    ///
    /// Returns an error when the mmap has an incorrect size.
    ///
    /// A (non-zero) header size in bytes may be provided to omit from the BitSlice data.
    ///
    /// # Panics
    ///
    /// - panics when the mmap data is not correctly aligned to the inner [`BitSlice`] type
    /// - panics when the header size isn't a multiple of the inner [`BitSlice`] type
    /// - See: [`mmap_to_slice_unbounded`]
    pub fn try_from(mut mmap: MmapMut, header_size: usize) -> Result<Self> {
        let data = unsafe { mmap_to_slice_unbounded(&mut mmap, header_size)? };
        let bitslice = BitSlice::from_slice_mut(data);
        let mmap = Arc::new(mmap);

        Ok(Self {
            mmap: MmapType {
                r#type: bitslice,
                mmap,
            },
        })
    }

    /// Get flusher to explicitly flush mmap at a later time
    pub fn flusher(&self) -> MmapFlusher {
        self.mmap.flusher()
    }

    pub fn create(path: &Path, bitslice: &BitSlice) -> Result<()> {
        let bits_count = bitslice.len();
        let bytes_count = bits_count
            .div_ceil(u8::BITS as usize)
            .next_multiple_of(Self::MIN_FILE_SIZE);

        let _file = ops::create_and_ensure_length(path, bytes_count)?;

        let mmap = ops::open_write_mmap(
            path,
            AdviceSetting::Advice(Advice::Normal), // We only write sequentially
            false,
        )?;

        let mut mmap_bitslice = MmapBitSlice::try_from(mmap, 0)?;

        mmap_bitslice.fill_with(|idx| {
            bitslice
                .get(idx)
                .map(|bitref| bitref.as_ref().to_owned())
                // mmap bitslice can be bigger than bitslice because it must align with size of `usize`
                .unwrap_or(false)
        });

        mmap_bitslice.flusher()()?;

        Ok(())
    }

    /// Populate all pages in the mmap.
    /// Block until all pages are populated.
    pub fn populate(&self) -> std::io::Result<()> {
        self.mmap.populate()?;
        Ok(())
    }

    /// Hint to the OS that pages backing this mmap can be reclaimed.
    pub fn clear_cache(&self) -> std::io::Result<()> {
        let Self { mmap } = self;
        mmap.clear_cache()?;
        Ok(())
    }
}

impl Deref for MmapBitSlice {
    type Target = BitSlice;

    fn deref(&self) -> &BitSlice {
        &self.mmap
    }
}

impl DerefMut for MmapBitSlice {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.mmap
    }
}

/// Typed mmap errors.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Mmap length must be {0} to match the size of type, but it is {1}")]
    SizeExact(usize, usize),
    #[error("Mmap length must be at least {0} to match the size of type, but it is {1}")]
    SizeLess(usize, usize),
    #[error("Mmap length must be multiple of {0} to match the size of type, but it is {1}")]
    SizeMultiple(usize, usize),
    #[error("{0}")]
    Io(#[from] std::io::Error),
    #[error("File not found: {0}")]
    MissingFile(String),
}

/// Get a second mutable reference for type `T` from the given mmap
///
/// # Warning
///
/// The returned reference is unbounded. The user must ensure it never outlives the `mmap` type.
///
/// # Safety
///
/// - unsafe because we create a second (unbounded) mutable reference
/// - malformed data in the mmap may break the transmuted type `T` resulting in undefined behavior
///
/// # Panics
///
/// - panics when the mmap data is not correctly aligned for type `T`
unsafe fn mmap_prefix_to_type_unbounded<'unbnd, T>(mmap: &mut MmapMut) -> Result<&'unbnd mut T>
where
    T: Sized,
{
    let size_t = mem::size_of::<T>();

    // Assert size
    if mmap.len() < size_t {
        return Err(Error::SizeLess(size_t, mmap.len()));
    }

    // Obtain unbounded bytes slice into mmap
    let bytes: &'unbnd mut [u8] = unsafe {
        let slice = mmap.deref_mut();
        slice::from_raw_parts_mut(slice.as_mut_ptr(), size_t)
    };

    // Assert alignment and size
    assert_alignment::<_, T>(bytes);

    #[cfg(debug_assertions)]
    if mmap.len() != size_t {
        log::warn!(
            "Mmap length {} is not equal to size of type {}",
            mmap.len(),
            size_t,
        );
    }

    #[cfg(debug_assertions)]
    if bytes.len() != mem::size_of::<T>() {
        return Err(Error::SizeExact(mem::size_of::<T>(), bytes.len()));
    }

    let ptr = bytes.as_mut_ptr().cast::<T>();
    Ok(unsafe { &mut *ptr })
}

/// Get a second mutable reference for a slice of type `T` from the given mmap
///
/// A (non-zero) header size in bytes may be provided to omit from the BitSlice data.
///
/// # Warning
///
/// The returned reference is unbounded. The user must ensure it never outlives the `mmap` type.
///
/// # Safety
///
/// - unsafe because we create a second (unbounded) mutable reference
/// - malformed data in the mmap may break the transmuted slice for type `T` resulting in undefined
///   behavior
///
/// # Panics
///
/// - panics when the mmap data is not correctly aligned for type `T`
/// - panics when the header size isn't a multiple of size `T`
unsafe fn mmap_to_slice_unbounded<'unbnd, T>(
    mmap: &mut MmapMut,
    header_size: usize,
) -> Result<&'unbnd mut [T]>
where
    T: Sized,
{
    let size_t = mem::size_of::<T>();

    // Assert size
    if size_t == 0 {
        // For zero-sized T, data part must be zero-sized as well, we cannot have infinite slice
        debug_assert_eq!(
            mmap.len().saturating_sub(header_size),
            0,
            "mmap data must be zero-sized, because size T is zero",
        );
    } else {
        // Must be multiple of size T
        debug_assert_eq!(header_size % size_t, 0, "header not multiple of size T");
        if !mmap.len().is_multiple_of(size_t) {
            return Err(Error::SizeMultiple(size_t, mmap.len()));
        }
    }

    // Obtain unbounded bytes slice into mmap
    let bytes: &'unbnd mut [u8] = unsafe {
        let slice = mmap.deref_mut();
        &mut slice::from_raw_parts_mut(slice.as_mut_ptr(), slice.len())[header_size..]
    };

    // Assert alignment and bytes size
    assert_alignment::<_, T>(bytes);
    debug_assert_eq!(bytes.len() + header_size, mmap.len());

    // Transmute slice types
    unsafe {
        Ok(slice::from_raw_parts_mut(
            bytes.as_mut_ptr().cast::<T>(),
            bytes.len().checked_div(size_t).unwrap_or(0),
        ))
    }
}

/// Assert slice `&[S]` is correctly aligned for type `T`.
///
/// # Panics
///
/// Panics when alignment is wrong.
fn assert_alignment<S, T>(bytes: &[S]) {
    assert_eq!(
        bytes.as_ptr().align_offset(mem::align_of::<T>()),
        0,
        "type must be aligned",
    );
}

#[cfg(test)]
mod tests {
    use std::fmt::Debug;
    use std::iter;

    use rand::rngs::{SmallRng, StdRng};
    use rand::{RngExt, SeedableRng};
    use tempfile::{Builder, NamedTempFile};

    use super::*;
    use crate::common::mmap::AdviceSetting;

    fn create_temp_mmap_file(len: usize) -> NamedTempFile {
        let tempfile = Builder::new()
            .prefix("test.")
            .suffix(".mmap")
            .tempfile()
            .unwrap();
        #[allow(clippy::disallowed_methods, reason = "test code")]
        tempfile.as_file().set_len(len as u64).unwrap();
        tempfile
    }

    #[test]
    fn test_open_zero_type() {
        check_open_zero_type::<()>(());
        check_open_zero_type::<u8>(0);
        check_open_zero_type::<usize>(0);
        check_open_zero_type::<f32>(0.0);
    }

    fn check_open_zero_type<T: Sized + PartialEq + Debug + 'static>(zero: T) {
        let bytes = mem::size_of::<T>();
        let tempfile = create_temp_mmap_file(bytes);
        let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();

        let mmap_type: MmapType<T> = unsafe { MmapType::from(mmap) };
        assert_eq!(mmap_type.deref(), &zero);
    }

    #[test]
    fn test_open_zero_slice() {
        check_open_zero_slice::<()>(0, ());
        check_open_zero_slice::<u8>(0, 0);
        check_open_zero_slice::<u8>(1, 0);
        check_open_zero_slice::<u8>(131, 0);
        check_open_zero_slice::<usize>(0, 0);
        check_open_zero_slice::<usize>(1, 0);
        check_open_zero_slice::<usize>(131, 0);
        check_open_zero_slice::<f32>(0, 0.0);
        check_open_zero_slice::<f32>(1, 0.0);
        check_open_zero_slice::<f32>(131, 0.0);
    }

    #[test]
    #[should_panic]
    fn test_open_zero_slice_infinite_length() {
        // A slice with zero-sized type T can never be more than 0 bytes
        check_open_zero_slice::<()>(1, ());
    }

    fn check_open_zero_slice<T: Sized + PartialEq + Debug + 'static>(len: usize, zero: T) {
        let bytes = mem::size_of::<T>() * len;
        let tempfile = create_temp_mmap_file(bytes);
        let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();

        let mmap_slice: MmapSlice<T> = unsafe { MmapSlice::from(mmap) };
        assert_eq!(mmap_slice.len(), len);
        assert!(mmap_slice.iter().all(|i| i == &zero));
    }

    #[test]
    fn test_reopen_random() {
        let mut rng = SmallRng::seed_from_u64(42);
        check_reopen_random::<(), _>(0, || rng.random());
        check_reopen_random::<u8, _>(0, || rng.random());
        check_reopen_random::<u8, _>(1, || rng.random());
        check_reopen_random::<u8, _>(131, || rng.random());
        check_reopen_random::<u64, _>(0, || rng.random());
        check_reopen_random::<u64, _>(1, || rng.random());
        check_reopen_random::<u64, _>(131, || rng.random());
        check_reopen_random::<f32, _>(0, || rng.random());
        check_reopen_random::<f32, _>(1, || rng.random());
        check_reopen_random::<f32, _>(131, || rng.random());
    }

    fn check_reopen_random<T, R>(len: usize, rng: R)
    where
        T: Sized + Copy + PartialEq + Debug + 'static,
        R: FnMut() -> T,
    {
        let bytes = mem::size_of::<T>() * len;
        let tempfile = create_temp_mmap_file(bytes);

        let template: Vec<T> = iter::repeat_with(rng).take(len).collect();

        // Write random values from template into mmap
        {
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let mut mmap_slice: MmapSlice<T> = unsafe { MmapSlice::from(mmap) };
            assert_eq!(mmap_slice.len(), len);
            mmap_slice.copy_from_slice(&template);
        }

        // Reopen and assert values from template
        {
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let mmap_slice: MmapSlice<T> = unsafe { MmapSlice::from(mmap) };
            assert_eq!(mmap_slice.as_ref(), template);
        }
    }

    #[test]
    fn test_bitslice() {
        check_bitslice_with_header(0, 0);
        check_bitslice_with_header(0, 128);
        check_bitslice_with_header(512, 0);
        check_bitslice_with_header(512, 256);
        check_bitslice_with_header(11721 * 8, 256);
    }

    fn check_bitslice_with_header(bits: usize, header_size: usize) {
        let bytes = (mem::size_of::<usize>() * bits / 8) + header_size;
        let tempfile = create_temp_mmap_file(bytes);

        // Fill bitslice
        {
            let mut rng = StdRng::seed_from_u64(42);
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let mut mmap_bitslice = MmapBitSlice::from(mmap, header_size);
            (0..bits).for_each(|i| mmap_bitslice.set(i, rng.random()));
        }

        // Reopen and assert contents
        {
            let mut rng = StdRng::seed_from_u64(42);
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let mmap_bitslice = MmapBitSlice::from(mmap, header_size);
            (0..bits).for_each(|i| assert_eq!(mmap_bitslice[i], rng.random::<bool>()));
        }
    }

    #[test]
    fn test_zero_sized_type() {
        {
            let tempfile = create_temp_mmap_file(0);
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let result = unsafe { MmapType::<()>::try_from(mmap).unwrap() };
            assert_eq!(result.deref(), &());
        }

        {
            let tempfile = create_temp_mmap_file(0);
            let mmap = ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
            let result = unsafe { MmapSlice::<()>::try_from(mmap).unwrap() };
            assert_eq!(result.as_ref(), &[]);
            assert_alignment::<_, ()>(result.as_ref());
        }
    }

    #[test]
    fn test_double_read_mmap() {
        // Create and open a tmp file
        // Mmap it with write access
        // then mmap it with read access
        // Check that the data is synchronized

        let tempfile = create_temp_mmap_file(1024);
        let mut mmap_write =
            ops::open_write_mmap(tempfile.path(), AdviceSetting::Global, false).unwrap();
        let mmap_read = ops::open_read_mmap(
            tempfile.path(),
            AdviceSetting::Advice(Advice::Sequential),
            false,
        )
        .unwrap();

        mmap_write[333] = 42;

        assert_eq!(mmap_read[333], 42);
    }
}