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
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::to_twos_complement::{ByteOrder, TwosComplement};
use crate::Arbi;
use alloc::vec::Vec;
impl Arbi {
/// Returns the memory representation of this integer as a byte [`Vec`] in
/// little-endian byte order, interpreted as a nonnegative integer.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(0x1234567890123456_u64);
///
/// let bytes = a.to_le_bytes();
/// assert_eq!(bytes, [0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]);
/// assert_eq!(bytes, 0x1234567890123456_u64.to_le_bytes());
/// ```
///
/// ## Complexity
/// \\( O(n) \\)
pub fn to_le_bytes(&self) -> Vec<u8> {
self.vec
.iter()
.flat_map(|digit| digit.to_le_bytes())
.collect()
}
/// Returns the memory representation of this integer as a byte [`Vec`] in
/// big-endian (network) byte order, interpreted as a nonnegative integer.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(0x1234567890123456_u64);
///
/// let bytes = a.to_be_bytes();
/// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]);
/// assert_eq!(bytes, 0x1234567890123456_u64.to_be_bytes());
/// ```
///
/// ## Complexity
/// \\( O(n) \\)
pub fn to_be_bytes(&self) -> Vec<u8> {
self.vec
.iter()
.rev()
.flat_map(|digit| digit.to_be_bytes())
.collect()
}
/// Returns the memory representation of this integer as a byte [`Vec`] in
/// little-endian byte order, interpreted as a signed integer.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(-0x1234567890123456_i64);
///
/// let bytes = a.to_le_bytes_signed();
/// assert_eq!(bytes, (-0x1234567890123456_i64).to_le_bytes());
/// ```
///
/// ## Complexity
/// \\( O(n) \\)
pub fn to_le_bytes_signed(&self) -> Vec<u8> {
let mut result = self.to_le_bytes();
if self.neg {
result.to_twos_complement(ByteOrder::Le);
}
result
}
/// Returns the memory representation of this integer as a byte [`Vec`] in
/// big-endian (network) byte order, interpreted as a signed integer.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(-0x1234567890123456_i64);
///
/// let bytes = a.to_be_bytes_signed();
/// assert_eq!(bytes, (-0x1234567890123456_i64).to_be_bytes());
/// ```
///
/// ## Complexity
/// \\( O(n) \\)
pub fn to_be_bytes_signed(&self) -> Vec<u8> {
let mut result = self.to_be_bytes();
if self.neg {
result.to_twos_complement(ByteOrder::Be);
}
result
}
/// Returns the memory representation of the absolute value of this integer
/// as a byte [`Vec`] in native byte order.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(0x1234567890123456_i64);
/// assert_eq!(a.to_ne_bytes(), 0x1234567890123456_i64.to_ne_bytes());
/// ```
pub fn to_ne_bytes(&self) -> Vec<u8> {
if cfg!(target_endian = "big") {
self.to_be_bytes()
} else {
self.to_le_bytes()
}
}
/// Returns the memory representation of this integer as a byte [`Vec`] in
/// native byte order.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(-0x1234567890123456_i64);
/// assert_eq!(
/// a.to_ne_bytes_signed(),
/// (-0x1234567890123456_i64).to_ne_bytes()
/// );
/// ```
pub fn to_ne_bytes_signed(&self) -> Vec<u8> {
if cfg!(target_endian = "big") {
self.to_be_bytes_signed()
} else {
self.to_le_bytes_signed()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
extern crate std;
use crate::util::test::{get_seedable_rng, get_uniform_die, Distribution};
use crate::{Digit, SDigit};
macro_rules! test_conv {
($rng:expr, $die:expr, $signed:ident) => {{
for _ in 0..i16::MAX {
let r = $die.sample($rng);
let a = Arbi::from(r);
if $signed {
assert_eq!(
r.to_le_bytes(),
a.to_le_bytes_signed().as_ref()
);
assert_eq!(
r.to_be_bytes(),
a.to_be_bytes_signed().as_ref()
);
assert_eq!(
r.to_ne_bytes(),
a.to_ne_bytes_signed().as_ref()
);
} else {
assert_eq!(r.to_le_bytes(), a.to_le_bytes().as_ref());
assert_eq!(r.to_be_bytes(), a.to_be_bytes().as_ref());
assert_eq!(r.to_ne_bytes(), a.to_ne_bytes().as_ref())
}
}
}};
}
#[test]
fn test_random_to_le_and_be_bytes() {
let (mut rng, _) = get_seedable_rng();
let die_i64 = get_uniform_die(i64::MIN, i64::MAX);
let die_i128 = get_uniform_die(i128::MIN, i128::MAX);
let die_u64 = get_uniform_die(u64::MIN, u64::MAX);
let die_u128 = get_uniform_die(u128::MIN, u128::MAX);
let die_digit = get_uniform_die(Digit::MIN, Digit::MAX);
let die_sdigit = get_uniform_die(SDigit::MIN, SDigit::MAX);
test_conv!(&mut rng, die_i64, true);
test_conv!(&mut rng, die_i128, true);
test_conv!(&mut rng, die_sdigit, true);
test_conv!(&mut rng, die_u64, false);
test_conv!(&mut rng, die_u128, false);
test_conv!(&mut rng, die_digit, false);
}
}