irox_bits/
allocimpls.rs

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
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

extern crate alloc;
use crate::bits::Bits;
use crate::error::Error;
use crate::mutbits::MutBits;
use crate::BitsWrapper;
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicU64, Ordering};

macro_rules! impl_bits_pop {
    ($($ty:tt)+) => {
        impl Bits for $($ty)+ {
            fn next_u8(&mut self) -> Result<Option<u8>, Error> {
                if self.is_empty() {
                    return Ok(None)
                }
                Ok(Some(self.remove(0) as u8))
            }

            fn read_some_into<T: MutBits>(&mut self, into: &mut T) -> Result<usize, Error> {
                Ok(into.write_some_bytes(self.as_ref()))
            }
        }
    };
}

impl_bits_pop!(String);
impl_bits_pop!(&mut String);
impl_bits_pop!(Vec<u8>);
impl_bits_pop!(&mut Vec<u8>);
macro_rules! impl_bits_vecdeque {
    ($($ty:tt)+) => {
        impl Bits for $($ty)+ {
            fn next_u8(&mut self) -> Result<Option<u8>, Error> {
                Ok(self.pop_front())
            }

            fn read_some_into<T: MutBits>(&mut self, into: &mut T) -> Result<usize, Error> {
                let mut wrote = 0;
                while let Some(val) = self.pop_front() {
                    let Ok(()) = into.write_u8(val) else {
                        return Ok(wrote);
                    };
                    wrote += 1;
                }
                Ok(wrote)
            }
        }
    };
}
impl_bits_vecdeque!(VecDeque<u8>);
impl_bits_vecdeque!(&mut VecDeque<u8>);

macro_rules! impl_mutbits_vecdeque {
    ($($ty:tt)+) => {
        impl MutBits for $($ty)+ {
            fn write_u8(&mut self, val: u8) -> Result<(), Error> {
                self.push_back(val);
                Ok(())
            }
        }
    };
}
impl_mutbits_vecdeque!(&mut VecDeque<u8>);
impl_mutbits_vecdeque!(VecDeque<u8>);

macro_rules! impl_push {
    ($cast:ty, $($ty:tt)+) => {
        impl MutBits for $($ty)+ {
            fn write_u8(&mut self, val: u8) -> Result<(), Error> {
                self.push(val as $cast);
                Ok(())
            }
        }
    };
}
impl_push!(char, &mut String);
impl_push!(char, String);
impl_push!(u8, Vec<u8>);
impl_push!(u8, &mut Vec<u8>);

impl<T: Bits> Bits for Box<T> {
    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
        T::next_u8(self)
    }
}
impl<T: MutBits> MutBits for Box<T> {
    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
        T::write_u8(self, val)
    }
}

///
/// A struct to count the number of bytes moving through it.
pub struct SharedCountingBits<'a, B> {
    inner: BitsWrapper<'a, B>,
    count: Arc<AtomicU64>,
}

impl<'a, B> SharedCountingBits<'a, B> {
    pub fn new(inner: BitsWrapper<'a, B>) -> Self {
        Self {
            inner,
            count: Arc::new(AtomicU64::new(0)),
        }
    }
    /// Get a reference to the counter
    pub fn get_count(&self) -> SharedROCounter {
        SharedROCounter::new(self.count.clone())
    }
}
impl<'a, B: Bits> Bits for SharedCountingBits<'a, B> {
    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
        let res = self.inner.next_u8();
        if let Ok(Some(_)) = &res {
            self.count.fetch_add(1, Ordering::Relaxed);
        }
        res
    }
}
impl<'a, B: MutBits> MutBits for SharedCountingBits<'a, B> {
    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
        self.inner.write_u8(val)?;
        self.count.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }
}

///
/// A Read-Only counter that can be shared between threads.  The owner of the underlying counter
/// is free to update the value, but users of this object alone may not.
#[derive(Debug, Clone)]
pub struct SharedROCounter {
    counter: Arc<AtomicU64>,
}

impl SharedROCounter {
    pub fn new(counter: Arc<AtomicU64>) -> Self {
        SharedROCounter { counter }
    }

    ///
    /// Returns the current value of the counter
    pub fn get_count(&self) -> u64 {
        self.counter.load(Ordering::Relaxed)
    }
}