Cargo fmt all the things

This commit is contained in:
Ignotus Peverell
2017-09-29 18:44:25 +00:00
parent 3b51180359
commit 8504efb796
57 changed files with 3678 additions and 2600 deletions
+25 -16
View File
@@ -22,27 +22,30 @@ use std::num;
/// Encode the provided bytes into a hex string
pub fn to_hex(bytes: Vec<u8>) -> String {
let mut s = String::new();
for byte in bytes {
write!(&mut s, "{:02x}", byte).expect("Unable to write");
}
s
let mut s = String::new();
for byte in bytes {
write!(&mut s, "{:02x}", byte).expect("Unable to write");
}
s
}
/// Decode a hex string into bytes.
pub fn from_hex(hex_str: String) -> Result<Vec<u8>, num::ParseIntError> {
let hex_trim = if &hex_str[..2] == "0x" {
hex_str[2..].to_owned()
} else {
hex_str.clone()
};
split_n(&hex_trim.trim()[..], 2).iter()
.map(|b| u8::from_str_radix(b, 16))
.collect::<Result<Vec<u8>, _>>()
let hex_trim = if &hex_str[..2] == "0x" {
hex_str[2..].to_owned()
} else {
hex_str.clone()
};
split_n(&hex_trim.trim()[..], 2)
.iter()
.map(|b| u8::from_str_radix(b, 16))
.collect::<Result<Vec<u8>, _>>()
}
fn split_n(s: &str, n: usize) -> Vec<&str> {
(0 .. (s.len() - n + 1)/2 + 1).map(|i| &s[2*i .. 2*i + n]).collect()
(0..(s.len() - n + 1) / 2 + 1)
.map(|i| &s[2 * i..2 * i + n])
.collect()
}
#[cfg(test)]
@@ -59,7 +62,13 @@ mod test {
#[test]
fn test_from_hex() {
assert_eq!(from_hex("00000000".to_string()).unwrap(), vec![0, 0, 0, 0]);
assert_eq!(from_hex("0a0b0c0d".to_string()).unwrap(), vec![10, 11, 12, 13]);
assert_eq!(from_hex("000000ff".to_string()).unwrap(), vec![0, 0, 0, 255]);
assert_eq!(
from_hex("0a0b0c0d".to_string()).unwrap(),
vec![10, 11, 12, 13]
);
assert_eq!(
from_hex("000000ff".to_string()).unwrap(),
vec![0, 0, 0, 255]
);
}
}
+14 -14
View File
@@ -28,26 +28,26 @@ pub use hex::*;
// (borrowed).
#[derive(Clone)]
pub struct OneTime<T> {
inner: RefCell<Option<T>>,
inner: RefCell<Option<T>>,
}
unsafe impl<T> Sync for OneTime<T> {}
unsafe impl<T> Send for OneTime<T> {}
impl<T> OneTime<T> {
/// Builds a new uninitialized OneTime.
pub fn new() -> OneTime<T> {
OneTime { inner: RefCell::new(None) }
}
/// Builds a new uninitialized OneTime.
pub fn new() -> OneTime<T> {
OneTime { inner: RefCell::new(None) }
}
/// Initializes the OneTime, should only be called once after construction.
pub fn init(&self, value: T) {
let mut inner_mut = self.inner.borrow_mut();
*inner_mut = Some(value);
}
/// Initializes the OneTime, should only be called once after construction.
pub fn init(&self, value: T) {
let mut inner_mut = self.inner.borrow_mut();
*inner_mut = Some(value);
}
/// Borrows the OneTime, should only be called after initialization.
pub fn borrow(&self) -> Ref<T> {
Ref::map(self.inner.borrow(), |o| o.as_ref().unwrap())
}
/// Borrows the OneTime, should only be called after initialization.
pub fn borrow(&self) -> Ref<T> {
Ref::map(self.inner.borrow(), |o| o.as_ref().unwrap())
}
}