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
// License: see LICENSE file at root directory of `master` branch

//! # Root

use std::{
    io::Read,
    mem,
    time::Duration,
};

use crate::Result;

/// # Identifier
///
/// [SHA3-512][wiki:SHA3] is intended to be used as IDs.
///
/// [wiki:SHA3]: https://en.wikipedia.org/wiki/SHA-3
pub type Id = [u8; 64];

/// # Default IP address, used for both server and client
pub const DEFAULT_IP: [u8; 4] = [127, 0, 0, 1];

/// # Default read/write timeout: 3 seconds
pub const DEFAULT_RW_TIMEOUT: Duration = Duration::from_secs(3);

/// # Compares IDs
pub fn cmp_ids(a: &Id, b: &Id) -> bool {
    for i in 0..a.len() {
        if a[i] != b[i] {
            return false;
        }
    }
    true
}

/// # Reads ID
pub fn read_id<T>(stream: &mut Read) -> Result<T> where T: From<Id> {
    let mut buf = [0_u8; mem::size_of::<Id>()];
    stream.read_exact(&mut buf)?;
    Ok(buf.into())
}

#[test]
fn test_id() {
    assert_eq!(mem::size_of::<Id>(), 64);
}