cloudburst 0.0.5

A library to help with the implementation of torrent based protocols and algorithms.
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
// Copyright 2022 Bryant Luk
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Data is exchanged in terms of `Piece`s and `Block`s.
//!
//! The torrent's data is split into consecutive `Piece`s. `Piece`s are referenced
//! by [`Index`]. Each piece has the same [`Length`] except possibly the last piece.
//!
//! Peers request and send data in terms of [`Block`]s. A `Block` is a range of
//! data within a single `Piece`. The `Block` specifies which piece via a piece
//! `Index`, the starting byte offset within the piece via a [`BlockBegin`], and
//! the length of the data requested via [`BlockLength`].
//!
//! Assume the metainfo is for a single 31MiB file. The metainfo specifies the
//! piece `Length` is 2MiB. There are 16 pieces with all pieces having a length
//! of 2MiB except the last piece which is only 1MiB.
//!
//! A peer may need piece `3` from another peer. The peer can request the first
//! 16KiB of the piece by sending a request for a `Block` with piece index `3`,
//! beginning at offset `0`, and with a length of 16KiB. Next, the peer can
//! request a `Block` with index `3`, beginning at offset `16KiB`, and a length
//! of 16KiB. Similar to pieces, blocks usually have the same length except for
//! the last block request in a piece. The peer continues requesting `Block`s
//! until it receives all of the data in the piece.
//!
//! The received data can be verified to be correct by data in the metainfo.

use core::{cmp::Ordering, fmt, num::TryFromIntError};

#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{vec, vec::Vec};
#[cfg(feature = "std")]
use std::{vec, vec::Vec};

/// A piece's index.
///
/// It is a zero based index.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Index(pub u32);

impl Index {
    /// Returns the current index - 1.
    #[inline]
    #[must_use]
    pub fn prev(&self) -> Option<Index> {
        self.0.checked_sub(1).map(Index)
    }
}

impl From<u32> for Index {
    fn from(value: u32) -> Self {
        Index(value)
    }
}

impl From<Index> for u32 {
    fn from(value: Index) -> Self {
        value.0
    }
}

impl From<Index> for u64 {
    fn from(value: Index) -> Self {
        u64::from(value.0)
    }
}

impl fmt::Display for Index {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A piece's length.
///
/// The length of all pieces in a torrent, except the last piece which may be smaller.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Length(pub u32);

impl From<u32> for Length {
    fn from(value: u32) -> Self {
        Length(value)
    }
}

impl From<Length> for u32 {
    fn from(value: Length) -> Self {
        value.0
    }
}

impl From<Length> for u64 {
    fn from(value: Length) -> Self {
        u64::from(value.0)
    }
}

impl TryFrom<Length> for usize {
    type Error = TryFromIntError;

    fn try_from(value: Length) -> Result<Self, Self::Error> {
        usize::try_from(value.0)
    }
}

impl fmt::Display for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl PartialEq<BlockBegin> for Length {
    fn eq(&self, other: &BlockBegin) -> bool {
        self.0 == other.0
    }
}

impl PartialOrd<BlockBegin> for Length {
    fn partial_cmp(&self, other: &BlockBegin) -> Option<Ordering> {
        Some(self.0.cmp(&other.0))
    }
}

/// A byte offset in a piece to begin the block at.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct BlockBegin(pub u32);

impl BlockBegin {
    /// Return the block's index in a piece if blocks begin at multiples of
    /// [`BlockLength::MAX`].
    #[must_use]
    pub const fn index(&self) -> u32 {
        self.0 / BlockLength::MAX
    }
}

impl From<u32> for BlockBegin {
    fn from(value: u32) -> Self {
        BlockBegin(value)
    }
}

impl From<BlockBegin> for u32 {
    fn from(value: BlockBegin) -> Self {
        value.0
    }
}

impl From<BlockBegin> for u64 {
    fn from(value: BlockBegin) -> Self {
        u64::from(value.0)
    }
}

impl TryFrom<BlockBegin> for usize {
    type Error = TryFromIntError;

    fn try_from(value: BlockBegin) -> Result<Self, Self::Error> {
        usize::try_from(value.0)
    }
}

impl PartialEq<Length> for BlockBegin {
    fn eq(&self, other: &Length) -> bool {
        self.0 == other.0
    }
}

impl PartialOrd<Length> for BlockBegin {
    fn partial_cmp(&self, other: &Length) -> Option<Ordering> {
        Some(self.0.cmp(&other.0))
    }
}

impl fmt::Display for BlockBegin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// A block's length.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct BlockLength(pub u32);

impl From<u32> for BlockLength {
    fn from(value: u32) -> Self {
        BlockLength(value)
    }
}

impl From<BlockLength> for u32 {
    fn from(value: BlockLength) -> Self {
        value.0
    }
}

impl From<BlockLength> for u64 {
    fn from(value: BlockLength) -> Self {
        u64::from(value.0)
    }
}

impl TryFrom<BlockLength> for usize {
    type Error = TryFromIntError;

    fn try_from(value: BlockLength) -> Result<Self, Self::Error> {
        usize::try_from(value.0)
    }
}

impl BlockLength {
    /// Maximum length of a block.
    pub const MAX: u32 = 16384;

    /// Maximum length of a block.
    pub const MAX_USIZE: usize = 16384;

    /// Maximum length of a block.
    #[must_use]
    pub const fn max() -> Self {
        BlockLength(16384)
    }

    /// If the block is greater than the max
    #[must_use]
    pub const fn is_greater_than_max(&self) -> bool {
        self.0 > Self::MAX
    }
}

impl fmt::Display for BlockLength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Identifies part of a piece.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Block {
    /// The piece's index
    pub index: Index,
    /// The starting byte offset within the piece
    pub begin: BlockBegin,
    /// The length of the block
    pub length: BlockLength,
}

impl From<(Index, BlockBegin, BlockLength)> for Block {
    fn from(value: (Index, BlockBegin, BlockLength)) -> Self {
        Block::new(value.0, value.1, value.2)
    }
}

impl Block {
    /// Instantiates a block.
    #[must_use]
    pub const fn new(index: Index, begin: BlockBegin, length: BlockLength) -> Self {
        Self {
            index,
            begin,
            length,
        }
    }
}

/// Data within a piece.
#[derive(Clone, PartialEq, Eq)]
pub struct BlockData<'a> {
    /// The piece's index
    pub index: Index,
    /// The starting byte offset within the piece
    pub begin: BlockBegin,
    /// The data starting at the `begin` offset within the piece `index`
    pub data: &'a [u8],
}

impl<'a> From<(Index, BlockBegin, &'a [u8])> for BlockData<'a> {
    fn from(value: (Index, BlockBegin, &'a [u8])) -> Self {
        BlockData::new(value.0, value.1, value.2)
    }
}

impl<'a> BlockData<'a> {
    /// Instantiates a block data.
    #[must_use]
    pub const fn new(index: Index, begin: BlockBegin, data: &'a [u8]) -> Self {
        Self { index, begin, data }
    }

    /// Returns a `Block` representation of the data.
    ///
    /// Useful when verifying a `BlockData` response matches a `Block` request.
    ///
    /// # Panics
    ///
    /// Panics if the data's length is greater than a [u32].
    #[must_use]
    pub fn to_block(&self) -> Block {
        Block {
            index: self.index,
            begin: self.begin,
            length: BlockLength::from(u32::try_from(self.data.len()).unwrap()),
        }
    }
}

impl<'a> fmt::Debug for BlockData<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BlockData")
            .field("index", &self.index)
            .field("begin", &self.begin)
            .field("data.len", &self.data.len())
            .finish()
    }
}

/// Bitfield has an incorrect length or has bits set beyond the maximum piece index.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[cfg_attr(feature = "std", error("invalid bitfield"))]
pub struct InvalidBitfieldError;

#[cfg(feature = "std")]
impl From<InvalidBitfieldError> for std::io::Error {
    fn from(error: InvalidBitfieldError) -> Self {
        std::io::Error::new(std::io::ErrorKind::InvalidData, error)
    }
}

/// Verifies a bitfield has the expected length and has no bits set beyond the maximum piece index.
///
/// # Errors
///
/// Returns an error if the bitfield is not the expected length or if it has
/// bits set beyond the maximum piece index.
///
/// # Panics
///
/// Panics if the expected bitfield length is greater than a [usize].
pub fn verify_bitfield<T: AsRef<[u8]>>(
    max_index: Index,
    bitfield: T,
) -> Result<(), InvalidBitfieldError> {
    let bitfield = bitfield.as_ref();

    let expected_bitfield_len =
        usize::try_from(max_index.0 / 8 + u32::from(max_index.0 % 8 != 0)).unwrap();
    if bitfield.len() != expected_bitfield_len {
        return Err(InvalidBitfieldError);
    }

    let remainder = usize::try_from(max_index.0 % 8).unwrap();
    if remainder != 0 {
        if let Some(last_byte) = bitfield.last() {
            for offset in remainder..8 {
                if (last_byte & 0x80 >> offset) != 0 {
                    return Err(InvalidBitfieldError);
                }
            }
        } else {
            return Err(InvalidBitfieldError);
        }
    }

    Ok(())
}

use bitvec::prelude::*;

/// A set of piece indexes.
///
/// Useful for identifying which pieces are available across all known peers or
/// by a specific peer.
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct IndexBitfield(BitVec<u8, Msb0>);

impl IndexBitfield {
    /// Instantiates an `IndexBitfield` with the maximum piece index.
    ///
    /// # Panics
    ///
    /// Panics if the maximum index is greater than a [usize].
    #[must_use]
    pub fn with_max_index(max_index: Index) -> Self {
        let len = usize::try_from(max_index.0).unwrap();
        Self(bitvec![u8, Msb0; 0; len])
    }

    /// Resizes the `IndexBitfield` for the maximum piece index and sets all values to false.
    ///
    /// # Panics
    ///
    /// Panics if the maximum index is greater than a [usize].
    pub fn clear_with_max_index(&mut self, max_index: Index) {
        self.0.clear();
        self.0.resize(usize::try_from(max_index.0).unwrap(), false);
    }

    /// Returns the maximum index for the bitfield.
    ///
    /// # Panics
    ///
    /// Panics if the bitfield length is greater than the maximum value of an [`Index`].
    #[must_use]
    pub fn max_index(&self) -> Index {
        Index::from(u32::try_from(self.0.len()).unwrap())
    }

    /// Sets the `IndexBitfield` with the raw bitfield bytes.
    ///
    /// # Important
    ///
    /// [`verify_bitfield`] should be called to verify the slice is a valid
    /// bitfield before calling this function
    ///
    /// # Panics
    ///
    /// Panics if the maximum index is greater than a [usize].
    #[must_use]
    pub fn from_slice(bitfield: &[u8], max_index: Index) -> Self {
        let mut bitfield = BitVec::from_slice(bitfield);
        bitfield.resize(usize::try_from(max_index.0).unwrap(), false);
        Self(bitfield)
    }

    /// View the underlying bytes of the index set.
    ///
    /// Useful when transmitting the bitfield message.
    #[must_use]
    pub fn as_raw_slice(&self) -> &[u8] {
        self.0.as_raw_slice()
    }

    /// Returns true if the index is set.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    #[must_use]
    pub fn get(&self, index: Index) -> bool {
        *self.0.get(usize::try_from(index.0).unwrap()).unwrap()
    }

    /// Sets the state for the given index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    pub fn set(&mut self, index: Index, value: bool) {
        *self.0.get_mut(usize::try_from(index.0).unwrap()).unwrap() = value;
    }

    /// If no piece indexes are set.
    #[must_use]
    pub fn not_any(&self) -> bool {
        self.0.not_any()
    }

    /// If not all piece indexes are set.
    #[must_use]
    pub fn not_all_set(&self) -> bool {
        self.0.not_all()
    }

    /// If all piece indexes are set.
    #[must_use]
    pub fn all_set(&self) -> bool {
        self.0.all()
    }

    /// Returns an iterator for piece indexes which are set.
    ///
    /// # Panics
    ///
    /// Panics if there is a set index in the bitfield where the index is
    /// greater than [u32].
    pub fn iter_set(&self) -> impl Iterator<Item = Index> + '_ {
        self.0
            .iter_ones()
            .map(|index| u32::try_from(index).unwrap())
            .map(Index)
    }

    #[cfg(test)]
    fn iter_not_set(&self) -> impl Iterator<Item = Index> + '_ {
        self.0
            .iter_zeros()
            .map(|index| u32::try_from(index).unwrap())
            .map(Index)
    }

    /// Calculates all of the piece indexes set in this `IndexBitfield` but are not set in the other `IndexBitfield`.
    #[must_use]
    pub fn difference(self, other: IndexBitfield) -> IndexBitfield {
        let orig = self.0;
        let not_other = !other.0;
        IndexBitfield(orig & not_other)
    }

    /// Determines if this `IndexBitfield` is a superset of the other `IndexBitfield`.
    ///
    /// # Panics
    ///
    /// Panics if the other bitfield is not the same length.
    #[must_use]
    pub fn is_superset(&self, other: &IndexBitfield) -> bool {
        assert_eq!(self.0.len(), other.0.len());
        for index in other.0.iter_ones() {
            if !*self.0.get(index).unwrap() {
                return false;
            }
        }
        true
    }

    /// Determines if this `IndexBitfield` is a subset of the other `IndexBitfield`.
    ///
    /// # Panics
    ///
    /// Panics if the other bitfield is not the same length.
    #[must_use]
    pub fn is_subset(&self, other: &IndexBitfield) -> bool {
        assert_eq!(self.0.len(), other.0.len());
        for index in self.0.iter_ones() {
            if !*other.0.get(index).unwrap() {
                return false;
            }
        }
        true
    }
}

/// Sealed trait for unsigned numbers.
///
/// Implemented for unsigned integers.
pub trait UnsignedInt: Sized + sealed::Private {
    /// Increments a number.
    ///
    /// # Panics
    ///
    /// Panics if the result is greater than the max value.
    #[must_use]
    fn inc(&self) -> Self;

    /// Decrements a number.
    ///
    /// # Panics
    ///
    /// Panics if the result is less than 0.
    #[must_use]
    fn dec(&self) -> Self;
}

/// A counted set of piece indexes.
///
/// Useful for keeping track of the number of peers which have a piece.
#[derive(Debug)]
pub struct IndexCountedSet<T>(Vec<T>);

impl<T> IndexCountedSet<T>
where
    T: UnsignedInt,
{
    /// Instantiates an `IndexCountedSet` with the maximum piece index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    #[must_use]
    pub fn new(max_index: Index) -> Self
    where
        T: Clone + Default,
    {
        let len = usize::try_from(max_index.0).unwrap();
        Self(vec![T::default(); len])
    }

    /// Returns the maximum index for the counted set.
    ///
    /// # Panics
    ///
    /// Panics if the length is greater than the maximum value of an [`Index`].
    #[must_use]
    pub fn max_index(&self) -> Index {
        Index::from(u32::try_from(self.0.len()).unwrap())
    }

    /// Increments the count for the given piece index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    pub fn inc(&mut self, index: Index)
    where
        T:,
    {
        let index = usize::try_from(index.0).unwrap();
        self.0[index] = self.0[index].inc();
    }

    /// Increments the count for all of the set bits in the given bitfield.
    ///
    /// # Important
    ///
    /// The `IndexBitfield` must represent the same number of pieces as this
    /// `IndexCountedSet.`
    ///
    /// # Panics
    ///
    /// Panics if the count becomes greater than a [u8].
    pub fn inc_bitfield(&mut self, bitfield: &IndexBitfield) {
        debug_assert_eq!(self.0.len(), bitfield.0.len());

        for index in bitfield.0.iter_ones() {
            self.0[index] = self.0[index].inc();
        }
    }

    /// Decrements the count for the given piece index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    ///
    /// Also panics if the count becomes less than 0.
    pub fn dec(&mut self, index: Index) {
        let index = usize::try_from(index.0).unwrap();
        self.0[index] = self.0[index].dec();
    }

    /// Decrements the count for all of the set bits in the given bitfield.
    ///
    /// # Important
    ///
    /// The `IndexBitfield` must represent the same number of pieces as this
    /// `IndexCountedSet.`
    ///
    /// # Panics
    ///
    /// Panics if the count becomes less than 0.
    pub fn dec_bitfield(&mut self, bitfield: &IndexBitfield) {
        debug_assert_eq!(self.0.len(), bitfield.0.len());

        for index in bitfield.0.iter_ones() {
            self.0[index] = self.0[index].dec();
        }
    }

    /// Sets the count to 0 for the given piece index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    pub fn reset(&mut self, index: Index)
    where
        T: Default,
    {
        let index = usize::try_from(index.0).unwrap();
        self.0[index] = T::default();
    }

    /// Returns the count for the given piece index.
    ///
    /// # Panics
    ///
    /// Panics if the index is greater than a [usize].
    pub fn count(&mut self, index: Index) -> T
    where
        T: Copy,
    {
        let index = usize::try_from(index.0).unwrap();
        self.0[index]
    }

    /// Returns an iterator which returns piece indexes with a non-zero count.
    ///
    /// # Panics
    ///
    /// Panics if an index is greater than a [u32].
    pub fn nonzero_iter(&self) -> impl Iterator<Item = Index> + '_
    where
        T: Copy + Default + PartialOrd,
    {
        self.0
            .iter()
            .enumerate()
            .filter(|(_, &req_count)| req_count > T::default())
            .map(|(index, _)| Index::from(u32::try_from(index).unwrap()))
    }
}

mod sealed {
    pub trait Private {}

    macro_rules! unsigned_int_impl {
        ($ty:ty) => {
            impl Private for $ty {}

            impl super::UnsignedInt for $ty {
                fn inc(&self) -> Self {
                    self.checked_add(1).unwrap()
                }

                fn dec(&self) -> Self {
                    self.checked_sub(1).unwrap()
                }
            }
        };
    }

    unsigned_int_impl!(u8);
    unsigned_int_impl!(u16);
    unsigned_int_impl!(u32);
    unsigned_int_impl!(u64);
    unsigned_int_impl!(usize);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_index_bitfield_max_index() {
        let max_piece_index = Index(9);
        let pieces_set = IndexBitfield::with_max_index(max_piece_index);
        assert_eq!(pieces_set.max_index(), Index(9));

        let pieces_set = IndexBitfield::from_slice(&[0; 2], Index(9));
        assert_eq!(pieces_set.max_index(), Index(9));
        assert_eq!(pieces_set.as_raw_slice(), &[0, 0]);

        let pieces_set = IndexBitfield::from_slice(&[0xFE; 20], Index(9));
        assert_eq!(pieces_set.max_index(), Index(9));
        assert_eq!(pieces_set.as_raw_slice(), &[0xFE, 0xFE]);
    }

    #[test]
    fn test_index_set() {
        let max_piece_index = Index(9);
        let mut pieces_set = IndexBitfield::with_max_index(max_piece_index);
        assert_eq!(pieces_set.0.as_raw_slice().len(), 2);

        assert_eq!(pieces_set.as_raw_slice()[0], 0x0);
        assert_eq!(pieces_set.as_raw_slice()[1], 0x0);
        assert_eq!(pieces_set.iter_set().count(), 0);
        assert_eq!(pieces_set.iter_not_set().count(), 9);

        assert!(!pieces_set.get(Index(3)));
        pieces_set.set(Index(3), true);
        assert_eq!(pieces_set.as_raw_slice()[0], 0x10);
        assert_eq!(pieces_set.as_raw_slice()[1], 0x00);
        assert!(pieces_set.get(Index(3)));
        assert_eq!(pieces_set.iter_set().count(), 1);
        assert_eq!(pieces_set.iter_not_set().count(), 8);

        assert!(!pieces_set.get(Index(8)));
        pieces_set.set(Index(8), true);
        assert_eq!(pieces_set.as_raw_slice()[0], 0x10);
        assert_eq!(pieces_set.as_raw_slice()[1], 0x80);
        assert!(pieces_set.get(Index(8)));
        assert_eq!(pieces_set.iter_set().count(), 2);
        assert_eq!(pieces_set.iter_not_set().count(), 7);

        assert!(pieces_set.get(Index(8)));
        pieces_set.set(Index(8), false);
        assert_eq!(pieces_set.as_raw_slice()[0], 0x10);
        assert_eq!(pieces_set.as_raw_slice()[1], 0x00);
        assert!(!pieces_set.get(Index(8)));
        assert_eq!(pieces_set.iter_set().count(), 1);
        assert_eq!(pieces_set.iter_not_set().count(), 8);
    }

    #[test]
    fn test_index_set_is_superset() {
        let mut pieces_set1 = IndexBitfield::with_max_index(Index(9));
        let mut pieces_set2 = IndexBitfield::with_max_index(Index(9));
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(pieces_set2.is_superset(&pieces_set1));

        pieces_set1.set(Index(1), true);
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(!pieces_set2.is_superset(&pieces_set1));

        pieces_set2.set(Index(1), true);
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(pieces_set2.is_superset(&pieces_set1));

        pieces_set1.set(Index(4), true);
        pieces_set1.set(Index(8), true);
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(!pieces_set2.is_superset(&pieces_set1));

        pieces_set2.set(Index(4), true);
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(!pieces_set2.is_superset(&pieces_set1));

        pieces_set2.set(Index(8), true);
        assert!(pieces_set1.is_superset(&pieces_set2));
        assert!(pieces_set2.is_superset(&pieces_set1));

        pieces_set2.set(Index(7), true);
        assert!(!pieces_set1.is_superset(&pieces_set2));
        assert!(pieces_set2.is_superset(&pieces_set1));

        pieces_set1.set(Index(6), true);
        assert!(!pieces_set1.is_superset(&pieces_set2));
        assert!(!pieces_set2.is_superset(&pieces_set1));
    }

    #[test]
    fn test_index_set_is_subset() {
        let mut pieces_set1 = IndexBitfield::with_max_index(Index(9));
        let mut pieces_set2 = IndexBitfield::with_max_index(Index(9));
        assert!(pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set1.set(Index(1), true);
        assert!(!pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set2.set(Index(1), true);
        assert!(pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set1.set(Index(4), true);
        pieces_set1.set(Index(8), true);
        assert!(!pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set2.set(Index(4), true);
        assert!(!pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set2.set(Index(8), true);
        assert!(pieces_set1.is_subset(&pieces_set2));
        assert!(pieces_set2.is_subset(&pieces_set1));

        pieces_set2.set(Index(7), true);
        assert!(pieces_set1.is_subset(&pieces_set2));
        assert!(!pieces_set2.is_subset(&pieces_set1));

        pieces_set1.set(Index(6), true);
        assert!(!pieces_set1.is_subset(&pieces_set2));
        assert!(!pieces_set2.is_subset(&pieces_set1));
    }
}