Remove unnecessary byte_utils helpers
[rust-lightning] / lightning / src / util / byte_utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 #[inline]
11 pub fn slice_to_be48(v: &[u8]) -> u64 {
12         ((v[0] as u64) << 8*5) |
13         ((v[1] as u64) << 8*4) |
14         ((v[2] as u64) << 8*3) |
15         ((v[3] as u64) << 8*2) |
16         ((v[4] as u64) << 8*1) |
17         ((v[5] as u64) << 8*0)
18 }
19 #[inline]
20 pub fn be48_to_array(u: u64) -> [u8; 6] {
21         assert!(u & 0xffff_0000_0000_0000 == 0);
22         let mut v = [0; 6];
23         v[0] = ((u >> 8*5) & 0xff) as u8;
24         v[1] = ((u >> 8*4) & 0xff) as u8;
25         v[2] = ((u >> 8*3) & 0xff) as u8;
26         v[3] = ((u >> 8*2) & 0xff) as u8;
27         v[4] = ((u >> 8*1) & 0xff) as u8;
28         v[5] = ((u >> 8*0) & 0xff) as u8;
29         v
30 }
31
32 #[cfg(test)]
33 mod tests {
34         use super::*;
35
36         #[test]
37         fn test_all() {
38                 assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
39                 assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
40         }
41 }