Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Replace byteorder with std methods
Browse files Browse the repository at this point in the history
There are a few places this ends up more verbose, but it removes a
dependency & source of unsafe code. The new-ish std methods cover
almost everything we were using byteorder for, in any case.

Signed-off-by: David Ross <David.Ross@wdc.com>
  • Loading branch information
David Ross authored and David Ross committed Jul 10, 2020
1 parent 577ad1a commit 7066bf5
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 27 deletions.
1 change: 0 additions & 1 deletion rubble-nrf5x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ version = "0.0.3"
edition = "2018"

[dependencies]
byteorder = { version = "1.3.2", default-features = false }
rubble = { path = "../rubble", version = "0.0.3", default-features = false }
nrf51-hal = { version = "0.10", optional = true, default-features = false }
nrf52810-hal = { version = "0.10", optional = true, default-features = false }
Expand Down
6 changes: 2 additions & 4 deletions rubble-nrf5x/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Useful utilities related to Rubble on the nRF52.

use byteorder::{ByteOrder, LittleEndian};

#[cfg(feature = "51")]
use nrf51_hal::pac;

Expand All @@ -27,8 +25,8 @@ pub fn get_device_address() -> DeviceAddress {
let mut devaddr = [0u8; 6];
let devaddr_lo = ficr.deviceaddr[0].read().bits();
let devaddr_hi = ficr.deviceaddr[1].read().bits() as u16;
LittleEndian::write_u32(&mut devaddr[..4], devaddr_lo);
LittleEndian::write_u16(&mut devaddr[4..], devaddr_hi);
devaddr[..4].copy_from_slice(&devaddr_lo.to_le_bytes());
devaddr[4..].copy_from_slice(&devaddr_hi.to_le_bytes());

// Address type
let devaddr_type = match ficr.deviceaddrtype.read().deviceaddrtype().variant() {
Expand Down
1 change: 0 additions & 1 deletion rubble/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ version = "0.0.3"
edition = "2018"

[dependencies]
byteorder = { version = "1.3.2", default-features = false }
bitflags = "1.2.1"
uuid = { version = "0.8.1", default-features = false }
heapless = "0.5.1"
Expand Down
19 changes: 6 additions & 13 deletions rubble/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! [`BytesOr`]: struct.BytesOr.html

use crate::Error;
use byteorder::{ByteOrder, LittleEndian};
use core::{cmp, fmt, iter, mem};

/// Reference to a `T`, or to a byte slice that can be decoded as a `T`.
Expand Down Expand Up @@ -354,29 +353,23 @@ impl<'a> ByteWriter<'a> {
/// If `self` does not have enough space left, an error will be returned and no bytes will be
/// written to `self`.
pub fn write_u16_le(&mut self, value: u16) -> Result<(), Error> {
let mut bytes = [0; 2];
LittleEndian::write_u16(&mut bytes, value);
self.write_slice(&bytes)
self.write_slice(&value.to_le_bytes())
}

/// Writes a `u32` to `self`, using Little Endian byte order.
///
/// If `self` does not have enough space left, an error will be returned and no bytes will be
/// written to `self`.
pub fn write_u32_le(&mut self, value: u32) -> Result<(), Error> {
let mut bytes = [0; 4];
LittleEndian::write_u32(&mut bytes, value);
self.write_slice(&bytes)
self.write_slice(&value.to_le_bytes())
}

/// Writes a `u64` to `self`, using Little Endian byte order.
///
/// If `self` does not have enough space left, an error will be returned and no bytes will be
/// written to `self`.
pub fn write_u64_le(&mut self, value: u64) -> Result<(), Error> {
let mut bytes = [0; 8];
LittleEndian::write_u64(&mut bytes, value);
self.write_slice(&bytes)
self.write_slice(&value.to_le_bytes())
}
}

Expand Down Expand Up @@ -495,19 +488,19 @@ impl<'a> ByteReader<'a> {
/// Reads a `u16` from `self`, using Little Endian byte order.
pub fn read_u16_le(&mut self) -> Result<u16, Error> {
let arr = self.read_array::<[u8; 2]>()?;
Ok(LittleEndian::read_u16(&arr))
Ok(u16::from_le_bytes(arr))
}

/// Reads a `u32` from `self`, using Little Endian byte order.
pub fn read_u32_le(&mut self) -> Result<u32, Error> {
let arr = self.read_array::<[u8; 4]>()?;
Ok(LittleEndian::read_u32(&arr))
Ok(u32::from_le_bytes(arr))
}

/// Reads a `u64` from `self`, using Little Endian byte order.
pub fn read_u64_le(&mut self) -> Result<u64, Error> {
let arr = self.read_array::<[u8; 8]>()?;
Ok(LittleEndian::read_u64(&arr))
Ok(u64::from_le_bytes(arr))
}
}

Expand Down
6 changes: 3 additions & 3 deletions rubble/src/link/advertising.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use crate::link::ad_structure::{AdStructure, Flags};
use crate::link::{channel_map::ChannelMap, AddressKind, DeviceAddress};
use crate::utils::{Hex, HexSlice};
use crate::{bytes::*, time::Duration, Error};
use byteorder::{ByteOrder, LittleEndian};
use core::{fmt, iter};
use core::{convert::TryInto, fmt, iter};

/// CRC initialization value for advertising channel packets.
///
Expand Down Expand Up @@ -694,7 +693,8 @@ impl Header {
}

pub fn parse(raw: &[u8]) -> Self {
Header(LittleEndian::read_u16(&raw))
let bytes: [u8; 2] = raw[..2].try_into().expect("raw has fewer than 2 bytes");
Header(u16::from_le_bytes(bytes))
}

/// Returns the raw representation of the header.
Expand Down
6 changes: 3 additions & 3 deletions rubble/src/link/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use crate::link::{llcp::ControlPdu, SeqNum};
use crate::{bytes::*, Error};
use byteorder::{ByteOrder, LittleEndian};
use core::fmt;
use core::{convert::TryInto, fmt};

/// 16-bit data channel header preceding the payload.
///
Expand Down Expand Up @@ -74,7 +73,8 @@ impl Header {
///
/// Panics when `raw` contains less than 2 Bytes.
pub fn parse(raw: &[u8]) -> Self {
Header(LittleEndian::read_u16(&raw))
let bytes: [u8; 2] = raw[..2].try_into().expect("raw has fewer than 2 bytes");
Header(u16::from_le_bytes(bytes))
}

/// Returns the raw representation of the header.
Expand Down
3 changes: 1 addition & 2 deletions rubble/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//! `1234ABCD-0000-1000-8000-00805F9B34FB`.

use crate::{bytes::*, Error};
use byteorder::{BigEndian, ByteOrder};
use core::fmt;

pub use uuid::Uuid;
Expand Down Expand Up @@ -53,7 +52,7 @@ impl Into<Uuid> for Uuid16 {
impl Into<Uuid> for Uuid32 {
fn into(self) -> Uuid {
let mut buf = BASE_UUID;
BigEndian::write_u32(&mut buf, self.0);
buf[..4].copy_from_slice(&self.0.to_be_bytes());
Uuid::from_bytes(buf)
}
}
Expand Down

0 comments on commit 7066bf5

Please sign in to comment.