namaste 0.2.1

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

//! # Root

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

/// # Unique ID
///
/// SHA3-512 is intended to be used as UIDs.
pub type Uid = [u8; UID_LEN];

/// # UID length
const UID_LEN: usize = 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 UIDs
pub fn cmp_uids(a: &Uid, b: &Uid) -> bool {
    for i in 0..a.len() {
        if a[i] != b[i] {
            return false;
        }
    }
    true
}

/// # Reads UID
pub fn read_uid<T>(stream: &mut TcpStream) -> io::Result<T> where T: From<Uid> {
    let mut buf = [0_u8; UID_LEN];
    stream.read_exact(&mut buf)?;
    Ok(buf.into())
}