aria2-protocol 0.3.8

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
//! Compressed bitfield storage for BitTorrent piece tracking.
//!
//! Uses 1 bit per piece instead of 1 byte (`Vec<bool>`), providing 8x memory reduction.
//! This is critical for large torrents with thousands of pieces.

/// A memory-efficient bitfield using 1 bit per element.
///
/// # Memory Efficiency
///
/// For a torrent with 10,000 pieces:
/// - `Vec<bool>`: 10,000 bytes
/// - `Bitfield`: 1,250 bytes (8x reduction)
///
/// # Examples
///
/// ```
/// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
///
/// let mut bf = Bitfield::new(100);
/// bf.set(5).unwrap();
/// assert!(bf.test(5));
/// assert!(!bf.test(6));
/// assert_eq!(bf.count_set(), 1);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bitfield {
    /// Storage for bits, 8 bits per byte
    bits: Vec<u8>,
    /// Total number of bits
    num_bits: usize,
}

impl Bitfield {
    /// Create a new bitfield with all bits unset (false).
    ///
    /// # Arguments
    ///
    /// * `num_bits` - Number of bits to store
    ///
    /// # Examples
    ///
    /// ```
    /// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
    ///
    /// let bf = Bitfield::new(100);
    /// assert_eq!(bf.len(), 100);
    /// assert!(bf.is_all_clear());
    /// ```
    pub fn new(num_bits: usize) -> Self {
        let num_bytes = num_bits.div_ceil(8);
        Bitfield {
            bits: vec![0u8; num_bytes],
            num_bits,
        }
    }

    /// Create a bitfield with all bits set (true).
    ///
    /// # Examples
    ///
    /// ```
    /// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
    ///
    /// let bf = Bitfield::all_set(50);
    /// assert!(bf.is_all_set());
    /// assert_eq!(bf.count_set(), 50);
    /// ```
    pub fn all_set(num_bits: usize) -> Self {
        let num_bytes = num_bits.div_ceil(8);
        let mut bits = vec![0xFFu8; num_bytes];

        // Clear unused bits in the last byte
        let remaining_bits = num_bits % 8;
        if remaining_bits > 0 && num_bytes > 0 {
            let mask = ((1u8 << remaining_bits) - 1) << (8 - remaining_bits);
            bits[num_bytes - 1] = mask;
        }

        Bitfield { bits, num_bits }
    }

    /// Create a bitfield from raw bytes (e.g., from a peer's bitfield message).
    ///
    /// # Arguments
    ///
    /// * `data` - Raw byte data
    /// * `num_bits` - Number of valid bits
    ///
    /// # Examples
    ///
    /// ```
    /// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
    ///
    /// // Bitfield with bits 0 and 7 set: 0b10000001 = 0x81
    /// let bf = Bitfield::from_bytes(&[0x81], 8);
    /// assert!(bf.test(0));
    /// assert!(bf.test(7));
    /// ```
    pub fn from_bytes(data: &[u8], num_bits: usize) -> Self {
        let num_bytes = num_bits.div_ceil(8);
        let mut bits = vec![0u8; num_bytes];

        let copy_len = std::cmp::min(data.len(), num_bytes);
        if copy_len > 0 {
            bits[..copy_len].copy_from_slice(&data[..copy_len]);
        }

        // Clear unused bits in the last byte
        let remaining_bits = num_bits % 8;
        if remaining_bits > 0 && num_bytes > 0 {
            let mask = ((1u8 << remaining_bits) - 1) << (8 - remaining_bits);
            bits[num_bytes - 1] &= mask;
        }

        Bitfield { bits, num_bits }
    }

    /// Get the raw byte representation.
    ///
    /// This is useful for sending bitfield messages to peers.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bits
    }

    /// Get the total number of bits.
    pub fn len(&self) -> usize {
        self.num_bits
    }

    /// Check if the bitfield is empty.
    pub fn is_empty(&self) -> bool {
        self.num_bits == 0
    }

    /// Set a bit to true.
    ///
    /// # Errors
    ///
    /// Returns `None` if index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
    ///
    /// let mut bf = Bitfield::new(10);
    /// assert!(bf.set(5).is_some());
    /// assert!(bf.set(100).is_none()); // Out of bounds
    /// ```
    pub fn set(&mut self, index: usize) -> Option<()> {
        if index >= self.num_bits {
            return None;
        }

        let byte_index = index / 8;
        let bit_offset = index % 8;
        self.bits[byte_index] |= 1 << (7 - bit_offset);
        Some(())
    }

    /// Clear a bit (set to false).
    ///
    /// # Errors
    ///
    /// Returns `None` if index is out of bounds.
    pub fn clear(&mut self, index: usize) -> Option<()> {
        if index >= self.num_bits {
            return None;
        }

        let byte_index = index / 8;
        let bit_offset = index % 8;
        self.bits[byte_index] &= !(1 << (7 - bit_offset));
        Some(())
    }

    /// Test if a bit is set.
    ///
    /// Returns `false` for out-of-bounds indices.
    ///
    /// # Examples
    ///
    /// ```
    /// use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
    ///
    /// let mut bf = Bitfield::new(10);
    /// bf.set(3).unwrap();
    /// assert!(bf.test(3));
    /// assert!(!bf.test(4));
    /// assert!(!bf.test(100)); // Out of bounds returns false
    /// ```
    pub fn test(&self, index: usize) -> bool {
        if index >= self.num_bits {
            return false;
        }

        let byte_index = index / 8;
        let bit_offset = index % 8;
        (self.bits[byte_index] & (1 << (7 - bit_offset))) != 0
    }

    /// Count the number of set bits.
    ///
    /// # Performance
    ///
    /// Uses `count_ones()` intrinsic for fast bit counting.
    pub fn count_set(&self) -> usize {
        let mut count = 0;

        // Count full bytes
        let full_bytes = self.num_bits / 8;
        for i in 0..full_bytes {
            count += self.bits[i].count_ones() as usize;
        }

        // Count remaining bits in the last partial byte
        let remaining_bits = self.num_bits % 8;
        if remaining_bits > 0 && full_bytes < self.bits.len() {
            let last_byte = self.bits[full_bytes];
            for bit in 0..remaining_bits {
                if last_byte & (1 << (7 - bit)) != 0 {
                    count += 1;
                }
            }
        }

        count
    }

    /// Count the number of clear bits.
    pub fn count_clear(&self) -> usize {
        self.num_bits - self.count_set()
    }

    /// Check if all bits are set.
    pub fn is_all_set(&self) -> bool {
        self.count_set() == self.num_bits
    }

    /// Check if all bits are clear.
    pub fn is_all_clear(&self) -> bool {
        self.bits.iter().all(|&b| b == 0)
    }

    /// Find the first set bit.
    ///
    /// Returns `None` if no bits are set.
    pub fn find_first_set(&self) -> Option<usize> {
        for (byte_index, &byte) in self.bits.iter().enumerate() {
            if byte != 0 {
                for bit_offset in 0..8 {
                    let index = byte_index * 8 + bit_offset;
                    if index < self.num_bits && (byte & (1 << (7 - bit_offset))) != 0 {
                        return Some(index);
                    }
                }
            }
        }
        None
    }

    /// Find the first clear bit.
    ///
    /// Returns `None` if all bits are set.
    pub fn find_first_clear(&self) -> Option<usize> {
        for (byte_index, &byte) in self.bits.iter().enumerate() {
            // Check if this byte has any clear bits (considering num_bits)
            let start_bit = byte_index * 8;
            let end_bit = std::cmp::min(start_bit + 8, self.num_bits);

            for bit_offset in 0..(end_bit - start_bit) {
                let index = start_bit + bit_offset;
                if (byte & (1 << (7 - bit_offset))) == 0 {
                    return Some(index);
                }
            }
        }
        None
    }

    /// Find the next set bit after the given index.
    ///
    /// Returns `None` if no more set bits exist.
    pub fn find_next_set(&self, after: usize) -> Option<usize> {
        let start = after + 1;
        if start >= self.num_bits {
            return None;
        }

        let byte_index = start / 8;
        let bit_offset = start % 8;

        // Check remaining bits in the current byte
        let byte = self.bits[byte_index];
        for bit in bit_offset..8 {
            let index = byte_index * 8 + bit;
            if index < self.num_bits && (byte & (1 << (7 - bit))) != 0 {
                return Some(index);
            }
        }

        // Check subsequent bytes
        for (bi, &b) in self.bits.iter().enumerate().skip(byte_index + 1) {
            if b != 0 {
                for bit_offset in 0..8 {
                    let index = bi * 8 + bit_offset;
                    if index < self.num_bits && (b & (1 << (7 - bit_offset))) != 0 {
                        return Some(index);
                    }
                }
            }
        }

        None
    }

    /// Get an iterator over all set bit indices.
    pub fn iter_set(&self) -> SetBitIter<'_> {
        SetBitIter::new(self)
    }

    /// Get an iterator over all clear bit indices.
    pub fn iter_clear(&self) -> ClearBitIter<'_> {
        ClearBitIter::new(self)
    }

    /// Calculate memory usage in bytes.
    ///
    /// This is the actual storage size, not including struct overhead.
    pub fn memory_usage(&self) -> usize {
        self.bits.len()
    }

    /// Calculate memory usage if using `Vec<bool>` for the same number of bits.
    ///
    /// This is useful for comparing memory savings.
    pub fn vec_bool_memory_usage(&self) -> usize {
        self.num_bits
    }

    /// Calculate memory savings ratio compared to `Vec<bool>`.
    ///
    /// Returns the ratio of `Vec<bool>` memory to Bitfield memory.
    /// A value of 8.0 means Bitfield uses 8x less memory.
    pub fn memory_savings_ratio(&self) -> f64 {
        if self.bits.is_empty() {
            return 1.0;
        }
        self.num_bits as f64 / self.bits.len() as f64
    }

    /// Set all bits to true.
    pub fn set_all(&mut self) {
        self.bits.fill(0xFF);

        // Clear unused bits in the last byte
        let remaining_bits = self.num_bits % 8;
        if remaining_bits > 0 && !self.bits.is_empty() {
            let mask = ((1u8 << remaining_bits) - 1) << (8 - remaining_bits);
            let last_idx = self.bits.len() - 1;
            self.bits[last_idx] = mask;
        }
    }

    /// Clear all bits.
    pub fn clear_all(&mut self) {
        self.bits.fill(0);
    }

    /// Perform a bitwise AND with another bitfield.
    ///
    /// Both bitfields must have the same length.
    pub fn bitand_assign(&mut self, other: &Bitfield) {
        assert_eq!(self.num_bits, other.num_bits, "Bitfield lengths must match");
        for (a, b) in self.bits.iter_mut().zip(other.bits.iter()) {
            *a &= b;
        }
    }

    /// Perform a bitwise OR with another bitfield.
    ///
    /// Both bitfields must have the same length.
    pub fn bitor_assign(&mut self, other: &Bitfield) {
        assert_eq!(self.num_bits, other.num_bits, "Bitfield lengths must match");
        for (a, b) in self.bits.iter_mut().zip(other.bits.iter()) {
            *a |= b;
        }
    }

    /// Perform a bitwise XOR with another bitfield.
    ///
    /// Both bitfields must have the same length.
    pub fn bitxor_assign(&mut self, other: &Bitfield) {
        assert_eq!(self.num_bits, other.num_bits, "Bitfield lengths must match");
        for (a, b) in self.bits.iter_mut().zip(other.bits.iter()) {
            *a ^= b;
        }
    }
}

/// Iterator over set bit indices.
pub struct SetBitIter<'a> {
    bitfield: &'a Bitfield,
    current: usize,
}

impl<'a> SetBitIter<'a> {
    fn new(bitfield: &'a Bitfield) -> Self {
        SetBitIter {
            bitfield,
            current: 0,
        }
    }
}

impl<'a> Iterator for SetBitIter<'a> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        while self.current < self.bitfield.num_bits {
            if self.bitfield.test(self.current) {
                let result = self.current;
                self.current += 1;
                return Some(result);
            }
            self.current += 1;
        }
        None
    }
}

/// Iterator over clear bit indices.
pub struct ClearBitIter<'a> {
    bitfield: &'a Bitfield,
    current: usize,
}

impl<'a> ClearBitIter<'a> {
    fn new(bitfield: &'a Bitfield) -> Self {
        ClearBitIter {
            bitfield,
            current: 0,
        }
    }
}

impl<'a> Iterator for ClearBitIter<'a> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        while self.current < self.bitfield.num_bits {
            if !self.bitfield.test(self.current) {
                let result = self.current;
                self.current += 1;
                return Some(result);
            }
            self.current += 1;
        }
        None
    }
}

#[cfg(test)]
#[path = "bitfield_tests.rs"]
mod tests;