divbuf 0.4.1

Buffer object that can be recursively divided into smaller buffers
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
// vim: tw=80

use std::{
    borrow::{Borrow, BorrowMut},
    cmp::Ordering,
    collections::hash_map::DefaultHasher,
    convert::TryInto,
    hash::{Hash, Hasher},
    io::Write,
    thread,
};

use divbuf::*;
use lazy_static::lazy_static;

fn simple_hash<T: Hash>(t: &T) -> u64 {
    let mut s = DefaultHasher::new();
    t.hash(&mut s);
    s.finish()
}

//
// Chunks methods
//
mod chunks {
    use super::*;

    #[test]
    pub fn iter() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db = dbs.try_const().unwrap();
        let mut chunks = db.into_chunks(3);
        assert_eq!(&chunks.next().unwrap()[..], &[1, 2, 3][..]);
        assert_eq!(&chunks.next().unwrap()[..], &[4, 5, 6][..]);
        assert!(chunks.next().is_none());
    }

    #[test]
    #[should_panic]
    pub fn zero() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db = dbs.try_const().unwrap();
        db.into_chunks(0);
    }

    #[test]
    pub fn size_hint() {
        let dbs =
            DivBufShared::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(1).size_hint(),
            (12, Some(12))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(2).size_hint(),
            (6, Some(6))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(3).size_hint(),
            (4, Some(4))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(4).size_hint(),
            (3, Some(3))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(5).size_hint(),
            (3, Some(3))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(6).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(7).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(8).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(9).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(10).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(11).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_const().unwrap().into_chunks(12).size_hint(),
            (1, Some(1))
        );
    }
}

//
// ChunksMut methods
//
mod chunks_mut {
    use super::*;

    #[test]
    pub fn iter() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let dbm = dbs.try_mut().unwrap();
        let mut chunks = dbm.into_chunks(3);
        assert_eq!(&chunks.next().unwrap()[..], &[1, 2, 3][..]);
        assert_eq!(&chunks.next().unwrap()[..], &[4, 5, 6][..]);
        assert!(chunks.next().is_none());
    }

    #[test]
    #[should_panic]
    pub fn zero() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let dbm = dbs.try_mut().unwrap();
        dbm.into_chunks(0);
    }

    #[test]
    pub fn size_hint() {
        let dbs =
            DivBufShared::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(1).size_hint(),
            (12, Some(12))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(2).size_hint(),
            (6, Some(6))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(3).size_hint(),
            (4, Some(4))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(4).size_hint(),
            (3, Some(3))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(5).size_hint(),
            (3, Some(3))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(6).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(7).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(8).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(9).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(10).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(11).size_hint(),
            (2, Some(2))
        );
        assert_eq!(
            dbs.try_mut().unwrap().into_chunks(12).size_hint(),
            (1, Some(1))
        );
    }
}

//
// DivBufShared methods
//
mod divbufshared {
    use super::*;

    #[test]
    pub fn cap_and_len() {
        let mut v = Vec::<u8>::with_capacity(64);
        v.push(0);
        let dbs = DivBufShared::from(v);
        assert_eq!(dbs.capacity(), 64);
        assert_eq!(dbs.len(), 1);
    }

    #[test]
    pub fn fmt() {
        let v = Vec::<u8>::with_capacity(64);
        let dbs = DivBufShared::from(v);
        let output = format!("{:?}", &dbs);
        let expected = "DivBufShared { inner: Inner { vec: [], accessors: 0, \
                        sharers: 1 } }";
        assert_eq!(output, expected);
    }

    #[test]
    pub fn from_slice() {
        let s = b"abcdefg";
        let dbs = DivBufShared::from(&s[..]);
        let mut dbm = dbs.try_mut().unwrap();
        assert_eq!(dbm, s[..]);
        // dbs should've been copy constructed, so we can mutate it without changing
        // the original slice
        dbm[0] = b'A';
        assert_ne!(dbm, s[..]);
    }

    #[test]
    pub fn is_empty() {
        assert!(DivBufShared::with_capacity(4096).is_empty());
        assert!(!DivBufShared::from(vec![1, 2, 3]).is_empty());
    }

    #[test]
    pub fn send() {
        let dbs = DivBufShared::with_capacity(4096);
        thread::spawn(move || {
            let _ = dbs;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn sync() {
        lazy_static! {
            pub static ref DBS: DivBufShared =
                DivBufShared::from(vec![0; 4096]);
        }
        let r = &DBS;
        thread::spawn(move || {
            let _ = r;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn try_const() {
        let dbs = DivBufShared::with_capacity(4096);
        // Create an initial DivBuf
        let _db0 = dbs.try_const().unwrap();
        // Creating a second is allowed, too
        let _db1 = dbs.try_const().unwrap();
    }

    #[test]
    pub fn try_const_after_try_mut() {
        let dbs = DivBufShared::with_capacity(4096);
        // Create an initial DivBufMut
        let _dbm = dbs.try_mut().unwrap();
        // Creating a DivBuf should fail, because there are writers
        assert!(dbs.try_const().is_err());
    }

    #[test]
    pub fn try_mut() {
        let dbs = DivBufShared::with_capacity(4096);
        // Create an initial DivBufMut
        let _dbm0 = dbs.try_mut().unwrap();
        // Creating a second is not allowed
        assert!(dbs.try_mut().is_err());
    }

    #[test]
    pub fn try_mut_after_try_const() {
        let dbs = DivBufShared::with_capacity(4096);
        // Create an initial DivBuf
        let _db0 = dbs.try_const().unwrap();
        // Now creating a mutable buffer is not allowed
        assert!(dbs.try_mut().is_err());
    }

    #[cfg(feature = "experimental")]
    #[test]
    pub fn uninitialized() {
        let cap = 4096;
        let dbs = DivBufShared::uninitialized(cap);
        assert_eq!(dbs.capacity(), cap);
        assert_eq!(dbs.len(), cap);
    }

    #[test]
    pub fn to_vec() {
        let v = vec![1, 2, 3, 4];
        let dbs = DivBufShared::from(v);

        {
            let mut dbm = dbs.try_mut().unwrap();
            assert_eq!(dbm, [1, 2, 3, 4][..]);
            dbm[0] = 5;
            assert_eq!(dbm, [5, 2, 3, 4][..]);
        }

        let v2: Vec<u8> = dbs.try_into().unwrap();
        assert_eq!(v2, vec![5, 2, 3, 4]);
    }

    #[test]
    pub fn to_vec_after_try_mut() {
        let v = vec![1, 2, 3, 4];
        let dbs = DivBufShared::from(v);

        let _dbm = dbs.try_mut().unwrap();
        let maybe_v: Result<Vec<u8>, _> = dbs.try_into();
        assert!(maybe_v.is_err());
    }

    #[test]
    pub fn to_vec_after_try_const() {
        let v = vec![1, 2, 3, 4];
        let dbs = DivBufShared::from(v);

        let _db = dbs.try_const().unwrap();
        let maybe_v: Result<Vec<u8>, _> = dbs.try_into();
        assert!(maybe_v.is_err());
    }

    #[test]
    pub fn to_vec_after_clone_inaccessible() {
        let v = vec![1, 2, 3, 4];
        let dbs = DivBufShared::from(v);

        let dbm = dbs.try_mut().unwrap();
        let _dbi = dbm.clone_inaccessible();
        drop(dbm);

        let maybe_v: Result<Vec<u8>, _> = dbs.try_into();
        assert!(maybe_v.is_err());
    }
}

//
// DivBuf methods
//
mod divbuf_ {
    use super::*;

    #[test]
    pub fn as_ref() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs.try_const().unwrap();
        let s: &[u8] = db0.as_ref();
        assert_eq!(s, &[1, 2, 3]);
    }

    #[test]
    pub fn as_ref_empty() {
        let dbs = DivBufShared::from(vec![]);
        let db0 = dbs.try_const().unwrap();
        let s: &[u8] = db0.as_ref();
        assert_eq!(s, &[]);
    }

    #[test]
    pub fn borrow() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs.try_const().unwrap();
        let s: &[u8] = db0.borrow();
        assert_eq!(s, &[1, 2, 3]);
    }

    #[test]
    pub fn clone() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs.try_const().unwrap();
        let mut db1 = db0.clone();
        assert_eq!(db0, db1);
        // We should be able to modify one DivBuf without affecting the other
        db1.split_off(1);
        assert_ne!(db0, db1);
    }

    #[test]
    pub fn clone_inaccessible() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let dbm = dbs.try_mut().unwrap();
        let _dbi: DivBufInaccessible = dbm.clone_inaccessible();
    }

    #[test]
    pub fn deref() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let db = dbs.try_const().unwrap();
        let slice: &[u8] = &db;
        assert_eq!(slice, &[1, 2, 3]);
    }

    #[test]
    pub fn deref_empty() {
        let dbs = DivBufShared::from(vec![]);
        let db = dbs.try_const().unwrap();
        let slice: &[u8] = &db;
        assert_eq!(slice, &[]);
    }

    // A DivBuf should be able to own its storage, and will free it on last drop
    #[test]
    pub fn drop_last() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let _db0 = dbs0.try_const().unwrap();
        drop(dbs0);
    }

    #[test]
    pub fn eq() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let dbs1 = DivBufShared::from(vec![1, 2, 3]);
        let dbs2 = DivBufShared::from(vec![1, 2]);
        let db0 = dbs0.try_const().unwrap();
        let db1 = dbs1.try_const().unwrap();
        let db2 = dbs2.try_const().unwrap();
        assert_eq!(db0, db1);
        assert_ne!(db0, db2);
    }

    #[test]
    pub fn from_divbufmut() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let dbm = dbs.try_mut().unwrap();
        let _db = DivBuf::from(dbm);
    }

    #[test]
    pub fn is_empty() {
        let dbs0 = DivBufShared::with_capacity(64);
        let db0 = dbs0.try_const().unwrap();
        assert!(db0.is_empty());

        let dbs1 = DivBufShared::from(vec![1]);
        let db1 = dbs1.try_const().unwrap();
        assert!(!db1.is_empty());
    }

    #[test]
    pub fn hash() {
        let v = vec![1, 2, 3, 4, 5, 6];
        let expected = simple_hash(&v);
        let dbs = DivBufShared::from(v);
        let db0 = dbs.try_const().unwrap();
        assert_eq!(simple_hash(&db0), expected);
    }

    #[test]
    pub fn ord() {
        let dbs = DivBufShared::from(vec![0, 1, 0, 2]);
        let db0 = dbs.try_const().unwrap().slice_to(2);
        let db1 = dbs.try_const().unwrap().slice_from(2);
        assert_eq!(db0.cmp(&db1), Ordering::Less);
    }

    #[test]
    pub fn partial_ord() {
        let dbs = DivBufShared::from(vec![0, 1, 0, 2]);
        let db0 = dbs.try_const().unwrap().slice_to(2);
        let db1 = dbs.try_const().unwrap().slice_from(2);
        assert!(db0 < db1);
    }

    #[test]
    pub fn send() {
        let dbs = DivBufShared::with_capacity(4096);
        let db = dbs.try_const().unwrap();
        thread::spawn(move || {
            let _ = db;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn sync() {
        lazy_static! {
            pub static ref DBS: DivBufShared =
                DivBufShared::from(vec![0; 4096]);
            pub static ref DB: DivBuf = DBS.try_const().unwrap();
        }
        let r = &DB;
        thread::spawn(move || {
            let _ = r;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn slice() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db0 = dbs.try_const().unwrap();
        assert_eq!(db0.slice(0, 0), [][..]);
        assert_eq!(db0.slice(1, 5), [2, 3, 4, 5][..]);
        assert_eq!(db0.slice(1, 1), [][..]);
        assert_eq!(db0.slice(0, 6), db0);
        assert_eq!(db0, [1, 2, 3, 4, 5, 6][..]);
    }

    #[test]
    #[should_panic(expected = "begin <= end")]
    pub fn slice_backwards() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db0 = dbs.try_const().unwrap();
        db0.slice(1, 0);
    }

    #[test]
    #[should_panic(expected = "end <= self.len")]
    pub fn slice_after_end() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db0 = dbs.try_const().unwrap();
        db0.slice(3, 7);
    }

    #[test]
    pub fn slice_from() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db0 = dbs.try_const().unwrap();
        assert_eq!(db0.slice_from(0), db0);
        assert_eq!(db0.slice_from(3), [4, 5, 6][..]);
    }

    #[test]
    pub fn slice_to() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let db0 = dbs.try_const().unwrap();
        assert_eq!(db0.slice_to(6), db0);
        assert_eq!(db0.slice_to(3), [1, 2, 3][..]);
    }

    #[test]
    pub fn split_off() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut db0 = dbs.try_const().unwrap();
        // split in the middle
        let db_mid = db0.split_off(4);
        assert_eq!(db0, [1, 2, 3, 4][..]);
        assert_eq!(db0.len(), 4);
        assert_eq!(db_mid, [5, 6][..]);
        assert_eq!(db_mid.len(), 2);
        // split at the beginning
        let mut db_begin = db0.split_off(0);
        assert_eq!(db0, [][..]);
        assert_eq!(db_begin, [1, 2, 3, 4][..]);
        // split at the end
        let db_end = db_begin.split_off(4);
        assert_eq!(db_begin, [1, 2, 3, 4][..]);
        assert_eq!(db_end, [][..]);
    }

    #[test]
    #[should_panic(expected = "Can't split past the end")]
    pub fn split_off_past_the_end() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut db0 = dbs.try_const().unwrap();
        db0.split_off(7);
    }

    #[test]
    pub fn split_to() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut db0 = dbs.try_const().unwrap();
        // split in the middle
        let mut db_left = db0.split_to(4);
        assert_eq!(db_left, [1, 2, 3, 4][..]);
        assert_eq!(db_left.len(), 4);
        assert_eq!(db0, [5, 6][..]);
        assert_eq!(db0.len(), 2);
        // split at the beginning
        let db_begin = db_left.split_to(0);
        assert_eq!(db_begin, [][..]);
        assert_eq!(db_left, [1, 2, 3, 4][..]);
        // split at the end
        let db_mid = db_left.split_to(4);
        assert_eq!(db_mid, [1, 2, 3, 4][..]);
        assert_eq!(db_left, [][..]);
    }

    #[test]
    #[should_panic(expected = "Can't split past the end")]
    pub fn split_to_past_the_end() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut db0 = dbs.try_const().unwrap();
        db0.split_to(7);
    }

    #[test]
    pub fn try_mut() {
        let dbs = DivBufShared::with_capacity(64);
        let mut db0 = dbs.try_const().unwrap();
        db0 = {
            let db1 = dbs.try_const().unwrap();
            // When multiple DivBufs are active, none can be upgraded
            let db2 = db0.try_mut();
            assert!(db2.is_err());
            let db3 = db1.try_mut();
            assert!(db3.is_err());
            db2.unwrap_err()
        };
        // A single DivBuf alone can be upgraded
        assert!(db0.try_mut().is_ok());
    }

    #[test]
    pub fn unsplit() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut db0 = dbs0.try_const().unwrap();
        {
            // split in the middle
            let db_mid = db0.split_off(4);
            // put it back together
            assert!(db0.unsplit(db_mid).is_ok());
            assert_eq!(db0, [1, 2, 3, 4, 5, 6][..]);
        }

        {
            // unsplit should fail for noncontiguous DivBufs
            let mut db_begin = db0.slice_to(2);
            let db_end = db0.slice_from(4);
            assert!(db_begin.unsplit(db_end).is_err());
        }

        {
            // unsplit should fail for overlapping DivBufs
            let mut db_begin = db0.slice_to(4);
            let db_end = db0.slice_from(2);
            assert!(db_begin.unsplit(db_end).is_err());
        }

        {
            // unsplit should fail for unrelated DivBufs
            let dbs1 = DivBufShared::from(vec![7, 8, 9]);
            let mut db_end = db0.slice_from(4);
            let db_unrelated = dbs1.try_const().unwrap();
            assert!(db_end.unsplit(db_unrelated).is_err());
        }
    }
}

mod divbuf_inaccessible {
    use super::*;

    /// DivBufInaccessible's superpower is clone.
    #[test]
    #[allow(clippy::redundant_clone)]
    pub fn clone() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs0.try_const().unwrap();
        let dbi0 = db0.clone_inaccessible();
        let _dbi1 = dbi0.clone();
    }

    // A DivBufInaccessible should be able to own its storage, and will free it
    // on last drop.
    #[test]
    pub fn drop_last() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs0.try_const().unwrap();
        let _dbi = db0.clone_inaccessible();
        drop(db0);
        drop(dbs0);
    }

    #[test]
    pub fn send() {
        let dbs = DivBufShared::with_capacity(4096);
        let db = dbs.try_const().unwrap();
        let dbi = db.clone_inaccessible();
        thread::spawn(move || {
            let _ = dbi;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn sync() {
        lazy_static! {
            pub static ref DBS: DivBufShared =
                DivBufShared::from(vec![0; 4096]);
            pub static ref DB: DivBuf = DBS.try_const().unwrap();
            pub static ref DBI: DivBufInaccessible = DB.clone_inaccessible();
        }
        let r = &DBI;
        thread::spawn(move || {
            let _ = r;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn try_const_failure() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let dbm = dbs0.try_mut().unwrap();
        let dbi = dbm.clone_inaccessible();
        dbi.try_const().unwrap_err();
    }

    #[test]
    pub fn try_const_success() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs0.try_const().unwrap();
        let dbi = db0.clone_inaccessible();
        dbi.try_const().unwrap();
    }

    #[test]
    pub fn try_mut_failure() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let db = dbs0.try_const().unwrap();
        let dbi = db.clone_inaccessible();
        dbi.try_mut().unwrap_err();
    }

    #[test]
    pub fn try_mut_success() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let db0 = dbs0.try_const().unwrap();
        let dbi = db0.clone_inaccessible();
        drop(db0);
        dbi.try_mut().unwrap();
    }
}

//
// DivBufMut methods
//
mod divbuf_mut {
    use super::*;

    #[test]
    pub fn as_ref() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let dbm0 = dbs.try_mut().unwrap();
        let s: &[u8] = dbm0.as_ref();
        assert_eq!(s, &[1, 2, 3]);
    }

    #[test]
    pub fn as_ref_empty() {
        let dbs = DivBufShared::from(vec![]);
        let dbm0 = dbs.try_mut().unwrap();
        let s: &[u8] = dbm0.as_ref();
        assert_eq!(s, &[]);
    }

    #[test]
    pub fn borrow() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let dbm0 = dbs.try_mut().unwrap();
        let s: &[u8] = dbm0.borrow();
        assert_eq!(s, &[1, 2, 3]);
    }

    #[test]
    pub fn borrowmut() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        {
            let mut dbm0 = dbs.try_mut().unwrap();
            let s: &mut [u8] = dbm0.borrow_mut();
            s[0] = 9;
        }
        let db0 = dbs.try_const().unwrap();
        let slice: &[u8] = &db0;
        assert_eq!(slice, &[9, 2, 3]);
    }

    #[test]
    pub fn clone_inaccessible() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let db = dbs.try_const().unwrap();
        let _dbi: DivBufInaccessible = db.clone_inaccessible();
    }

    #[test]
    pub fn deref() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let dbm = dbs.try_mut().unwrap();
        let slice: &[u8] = &dbm;
        assert_eq!(slice, &[1, 2, 3]);
    }

    #[test]
    pub fn deref_empty() {
        let dbs = DivBufShared::from(vec![]);
        let dbm = dbs.try_mut().unwrap();
        let slice: &[u8] = &dbm;
        assert_eq!(slice, &[]);
    }

    #[test]
    pub fn derefmut() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        let mut dbm = dbs.try_mut().unwrap();
        // Unlike DivBuf, we _can_ update DivBufMuts randomly
        dbm[0] = 9;
        let slice: &mut [u8] = &mut dbm;
        assert_eq!(slice, &[9, 2, 3]);
    }

    #[test]
    pub fn derefmut_empty() {
        let dbs = DivBufShared::from(vec![]);
        let mut dbm = dbs.try_mut().unwrap();
        let slice: &mut [u8] = &mut dbm;
        assert_eq!(slice, &[]);
    }

    // A DivBufMut should be able to own its storage, and will free it on last drop
    #[test]
    pub fn drop_last() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let _dbm0 = dbs0.try_mut().unwrap();
        drop(dbs0);
    }

    #[test]
    pub fn eq() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3]);
        let dbs1 = DivBufShared::from(vec![1, 2, 3]);
        let dbs2 = DivBufShared::from(vec![1, 2]);
        let dbm0 = dbs0.try_mut().unwrap();
        let dbm1 = dbs1.try_mut().unwrap();
        let dbm2 = dbs2.try_mut().unwrap();
        assert_eq!(dbm0, dbm1);
        assert_ne!(dbm0, dbm2);
    }

    #[test]
    pub fn extend() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        {
            let mut dbm = dbs.try_mut().unwrap();
            dbm.extend([4, 5, 6].iter());
        }
        // verify that dbs.inner.vec was extended
        let db = dbs.try_const().unwrap();
        let slice: &[u8] = &db;
        assert_eq!(slice, &[1, 2, 3, 4, 5, 6]);
    }

    #[test]
    #[should_panic(expected = "extend into the middle of a buffer")]
    pub fn extend_from_the_middle() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut dbm = dbs.try_mut().unwrap();
        let mut dbm_begin = dbm.split_to(3);
        dbm_begin.extend([7, 8, 9].iter());
    }

    #[test]
    pub fn freeze() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6, 7, 8]);
        {
            // Simplest case: freeze the entire buffer
            let dbm = dbs.try_mut().unwrap();
            let _: DivBuf = dbm.freeze();
        }
        {
            // Freeze a buffer in the presence of other readers && writers
            let mut dbm = dbs.try_mut().unwrap();
            let right_half = dbm.split_off(4);
            let _db_right_half = right_half.freeze();
            let left_quarter = dbm.split_to(2);
            let _db_left_quarter = left_quarter.freeze();
            // We should still be able to mutate from the remaining DivBufMut
            dbm[0] = 33;
        }
    }

    #[test]
    pub fn hash() {
        let v = vec![1, 2, 3, 4, 5, 6];
        let expected = simple_hash(&v);
        let dbs = DivBufShared::from(v);
        let dbm0 = dbs.try_mut().unwrap();
        assert_eq!(simple_hash(&dbm0), expected);
    }

    #[test]
    pub fn is_empty() {
        let dbs0 = DivBufShared::with_capacity(64);
        let mut dbm0 = dbs0.try_mut().unwrap();
        assert!(dbm0.is_empty());

        dbm0.extend([4, 5, 6].iter());
        assert!(!dbm0.is_empty());
    }

    #[test]
    pub fn ord() {
        let dbs = DivBufShared::from(vec![0, 1, 0, 2]);
        let mut dbm0 = dbs.try_mut().unwrap();
        let dbm1 = dbm0.split_off(2);
        assert_eq!(dbm0.cmp(&dbm1), Ordering::Less);
    }

    #[test]
    pub fn partial_ord() {
        let dbs = DivBufShared::from(vec![0, 1, 0, 2]);
        let mut dbm0 = dbs.try_mut().unwrap();
        let dbm1 = dbm0.split_off(2);
        assert!(dbm0 < dbm1);
    }

    #[test]
    pub fn reserve() {
        let v = Vec::<u8>::with_capacity(64);
        let dbs = DivBufShared::from(v);
        let mut dbm = dbs.try_mut().unwrap();
        dbm.reserve(128);
        assert_eq!(dbs.capacity(), 128);
    }

    #[test]
    #[should_panic(expected = "reserve from the middle of a buffer")]
    pub fn reserve_from_the_middle() {
        let v = vec![1, 2, 3, 4, 5, 6];
        let dbs = DivBufShared::from(v);
        let mut dbm = dbs.try_mut().unwrap();
        let mut left_half = dbm.split_to(3);
        left_half.reserve(128);
    }

    #[test]
    pub fn send() {
        let dbs = DivBufShared::with_capacity(4096);
        let dbm = dbs.try_mut().unwrap();
        thread::spawn(move || {
            let _ = dbm;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn sync() {
        lazy_static! {
            pub static ref DBS: DivBufShared =
                DivBufShared::from(vec![0; 4096]);
            pub static ref DBM: DivBufMut = DBS.try_mut().unwrap();
        }
        let r = &DBM;
        thread::spawn(move || {
            let _ = r;
        })
        .join()
        .unwrap();
    }

    #[test]
    pub fn split_off() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut dbm0 = dbs.try_mut().unwrap();
        // split in the middle
        let dbm_mid = dbm0.split_off(4);
        assert_eq!(dbm0, [1, 2, 3, 4][..]);
        assert_eq!(dbm0.len(), 4);
        assert_eq!(dbm_mid, [5, 6][..]);
        assert_eq!(dbm_mid.len(), 2);
        // split at the beginning
        let mut dbm_begin = dbm0.split_off(0);
        assert_eq!(dbm0, [][..]);
        assert_eq!(dbm_begin, [1, 2, 3, 4][..]);
        // split at the end
        let dbm_end = dbm_begin.split_off(4);
        assert_eq!(dbm_begin, [1, 2, 3, 4][..]);
        assert_eq!(dbm_end, [][..]);
    }

    #[test]
    #[should_panic(expected = "Can't split past the end")]
    pub fn split_off_past_the_end() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut dbm0 = dbs.try_mut().unwrap();
        dbm0.split_off(7);
    }

    #[test]
    pub fn split_to() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut dbm0 = dbs.try_mut().unwrap();
        // split in the middle
        let mut dbm_left = dbm0.split_to(4);
        assert_eq!(dbm_left, [1, 2, 3, 4][..]);
        assert_eq!(dbm_left.len(), 4);
        assert_eq!(dbm0, [5, 6][..]);
        assert_eq!(dbm0.len(), 2);
        // split at the beginning
        let dbm_begin = dbm_left.split_to(0);
        assert_eq!(dbm_begin, [][..]);
        assert_eq!(dbm_left, [1, 2, 3, 4][..]);
        // split at the end
        let dbm_mid = dbm_left.split_to(4);
        assert_eq!(dbm_mid, [1, 2, 3, 4][..]);
        assert_eq!(dbm_left, [][..]);
    }

    #[test]
    #[should_panic(expected = "Can't split past the end")]
    pub fn split_to_past_the_end() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        let mut dbm0 = dbs.try_mut().unwrap();
        dbm0.split_to(7);
    }

    #[test]
    pub fn try_extend() {
        let dbs = DivBufShared::from(vec![1, 2, 3]);
        {
            let mut dbm0 = dbs.try_mut().unwrap();
            assert!(dbm0.try_extend([4, 5, 6].iter()).is_ok());

            // Extending from the middle of the vec should fail
            let mut dbm1 = dbm0.split_to(2);
            assert!(dbm1.try_extend([7, 8, 9].iter()).is_err());
        }

        // verify that dbs.inner.vec was extended the first time, but not the
        // second.
        let db = dbs.try_const().unwrap();
        assert_eq!(db, [1, 2, 3, 4, 5, 6][..]);
    }

    #[test]
    pub fn try_resize() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        {
            let mut dbm0 = dbs.try_mut().unwrap();
            // First, resize past the end of the vector
            assert!(dbm0.try_resize(7, 42).is_ok());
            assert_eq!(dbm0.len(), 7);
            assert_eq!(&dbm0[..], &[1, 2, 3, 4, 5, 6, 42][..]);
            // Then, do a truncation
            assert!(dbm0.try_resize(4, 42).is_ok());
            assert_eq!(dbm0, [1, 2, 3, 4][..]);
            // Check that the shared vector was truncated, too
            assert_eq!(dbs.len(), 4);
            // A resize of a non-terminal DivBufMut should fail
            let mut dbm1 = dbm0.split_to(2);
            assert!(dbm1.try_resize(3, 42).is_err());
            assert!(dbm1.try_resize(10, 42).is_err());
            assert_eq!(dbs.len(), 4);
            // Resizing a terminal DivBufMut should work, even if it doesn't start
            // at the vector's beginning
            assert!(dbm0.try_resize(5, 0).is_ok());
            assert_eq!(&dbm0[..], &[3, 4, 0, 0, 0][..]);
        }
    }

    #[test]
    pub fn try_truncate() {
        let dbs = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        {
            let mut dbm0 = dbs.try_mut().unwrap();
            // First, truncate past the end of the vector
            assert!(dbm0.try_truncate(7).is_ok());
            assert_eq!(dbm0.len(), 6);
            // Then, do a normal truncation
            assert!(dbm0.try_truncate(4).is_ok());
            assert_eq!(dbm0, [1, 2, 3, 4][..]);
            // Check that the shared vector was truncated, too
            assert_eq!(dbs.len(), 4);
            // A truncation of a non-terminal DivBufMut should fail
            let mut dbm1 = dbm0.split_to(2);
            assert!(dbm1.try_truncate(3).is_err());
            assert_eq!(dbs.len(), 4);
            // Truncating a terminal DivBufMut should work, even if it doesn't start
            // at the vector's beginning
            assert!(dbm0.try_truncate(1).is_ok());
            assert_eq!(dbs.len(), 3);
        }
    }

    #[test]
    pub fn unsplit() {
        let dbs0 = DivBufShared::from(vec![1, 2, 3, 4, 5, 6]);
        {
            let mut dbm0 = dbs0.try_mut().unwrap();
            // split in the middle
            let dbm_mid = dbm0.split_off(4);
            // put it back together
            assert!(dbm0.unsplit(dbm_mid).is_ok());
            assert_eq!(dbm0, [1, 2, 3, 4, 5, 6][..]);
        }

        {
            // unsplit should fail for noncontiguous DivBufMuts
            let mut dbm0 = dbs0.try_mut().unwrap();
            let mut dbm_begin = dbm0.split_to(2);
            let dbm_end = dbm0.split_off(2);
            assert!(dbm_begin.unsplit(dbm_end).is_err());
        }

        {
            // unsplit should fail for unrelated DivBufMuts
            let mut dbm0 = dbs0.try_mut().unwrap();
            let dbs1 = DivBufShared::from(vec![7, 8, 9]);
            let mut dbm_end = dbm0.split_off(4);
            let dbm_unrelated = dbs1.try_mut().unwrap();
            assert!(dbm_end.unsplit(dbm_unrelated).is_err());
        }
    }

    #[test]
    pub fn write() {
        const MSG: &[u8] = b"ABCD";
        let dbs0 = DivBufShared::with_capacity(0);
        let mut dbm0 = dbs0.try_mut().unwrap();
        assert_eq!(MSG.len(), dbm0.write(MSG).unwrap());
        assert_eq!(&dbm0[..], &[65u8, 66u8, 67u8, 68u8][..])
    }

    #[test]
    pub fn write_nonterminal() {
        let dbs0 = DivBufShared::from(vec![0, 1, 2, 3]);
        let mut dbm0 = dbs0.try_mut().unwrap();
        let _ = dbm0.split_off(2);
        assert!(dbm0.write("ABCD".as_bytes()).is_err());
    }

    #[test]
    pub fn write_all() {
        let dbs0 = DivBufShared::with_capacity(0);
        let mut dbm0 = dbs0.try_mut().unwrap();
        dbm0.write_all("ABCD".as_bytes()).unwrap();
        assert_eq!(&dbm0[..], &[65u8, 66u8, 67u8, 68u8][..])
    }

    #[test]
    pub fn write_all_nonterminal() {
        let dbs0 = DivBufShared::from(vec![0, 1, 2, 3]);
        let mut dbm0 = dbs0.try_mut().unwrap();
        let _ = dbm0.split_off(2);
        assert!(dbm0.write_all("ABCD".as_bytes()).is_err());
    }

    #[test]
    pub fn flush() {
        let dbs0 = DivBufShared::with_capacity(0);
        let mut dbm0 = dbs0.try_mut().unwrap();
        dbm0.write_all("ABCD".as_bytes()).unwrap();
        dbm0.flush().unwrap();
        assert_eq!(&dbm0[..], &[65u8, 66u8, 67u8, 68u8][..])
    }
}