2015-06-23 14:38:29 +00:00
|
|
|
use rand::{Rng,Rand};
|
2015-07-02 19:42:49 +00:00
|
|
|
use time;
|
2015-07-07 21:40:31 +00:00
|
|
|
use std::io;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
mod int128;
|
|
|
|
mod spotify_id;
|
|
|
|
mod arcvec;
|
2015-06-23 17:34:48 +00:00
|
|
|
mod subfile;
|
2015-07-07 21:40:31 +00:00
|
|
|
mod zerofile;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
pub use util::int128::u128;
|
|
|
|
pub use util::spotify_id::{SpotifyId, FileId};
|
|
|
|
pub use util::arcvec::ArcVec;
|
2015-06-23 17:34:48 +00:00
|
|
|
pub use util::subfile::Subfile;
|
2015-07-07 21:40:31 +00:00
|
|
|
pub use util::zerofile::ZeroFile;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! eprintln(
|
|
|
|
($($arg:tt)*) => (
|
|
|
|
{
|
|
|
|
use std::io::Write;
|
|
|
|
writeln!(&mut ::std::io::stderr(), $($arg)* ).unwrap()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! eprint(
|
|
|
|
($($arg:tt)*) => (
|
|
|
|
{
|
|
|
|
use std::io::Write;
|
|
|
|
write!(&mut ::std::io::stderr(), $($arg)* ).unwrap()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
pub fn rand_vec<G: Rng, R: Rand>(rng: &mut G, size: usize) -> Vec<R> {
|
|
|
|
let mut vec = Vec::with_capacity(size);
|
|
|
|
|
|
|
|
for _ in 0..size {
|
|
|
|
vec.push(R::rand(rng));
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod version {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/version.rs"));
|
|
|
|
|
|
|
|
pub fn version_string() -> String {
|
|
|
|
format!("librespot-{}", short_sha())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hexdump(data: &[u8]) {
|
|
|
|
for b in data.iter() {
|
|
|
|
eprint!("{:02X} ", b);
|
|
|
|
}
|
|
|
|
eprintln!("");
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
pub trait IgnoreExt {
|
|
|
|
fn ignore(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T, E> IgnoreExt for Result<T, E> {
|
|
|
|
fn ignore(self) {
|
|
|
|
match self {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 19:42:49 +00:00
|
|
|
|
|
|
|
pub fn now_ms() -> i64 {
|
|
|
|
let ts = time::now_utc().to_timespec();
|
|
|
|
ts.sec * 1000 + ts.nsec as i64 / 1000000
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:40:31 +00:00
|
|
|
pub fn mkdir_existing(path: &Path) -> io::Result<()> {
|
|
|
|
fs::create_dir(path)
|
|
|
|
.or_else(|err| if err.kind() == io::ErrorKind::AlreadyExists {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|