Skip to main content

twine_codec/radio/
extended_address.rs

1// Copyright (c) 2025 Jake Swensen
2// SPDX-License-Identifier: MPL-2.0
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8const EXTENDED_ADDRESS_SIZE: usize = 8;
9
10pub struct ExtendedAddress([u8; EXTENDED_ADDRESS_SIZE]);
11
12impl ExtendedAddress {
13    pub fn random() -> Self {
14        let mut bytes = [0u8; EXTENDED_ADDRESS_SIZE];
15        crate::fill_random_bytes(&mut bytes);
16        Self(bytes)
17    }
18}
19
20impl From<ExtendedAddress> for u64 {
21    fn from(value: ExtendedAddress) -> Self {
22        u64::from_be_bytes(value.0)
23    }
24}
25
26impl From<u64> for ExtendedAddress {
27    fn from(extended_address: u64) -> Self {
28        Self(extended_address.to_be_bytes())
29    }
30}
31
32impl core::fmt::Display for ExtendedAddress {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        for byte in &self.0 {
35            write!(f, "{:02x}", byte)?;
36        }
37
38        Ok(())
39    }
40}