namaste 0.9.0

Simple locks between processes
Documentation
// License: see LICENSE file at root directory of `master` branch

//! # Root

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

/// # 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) -> io::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);
}