mod.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. use num_bigint::BigUint;
  2. use num_traits::{Zero, One};
  3. use num_integer::Integer;
  4. use rand::{Rng, Rand};
  5. use std::io;
  6. use std::mem;
  7. use std::ops::{Mul, Rem, Shr};
  8. use std::fs;
  9. use std::path::Path;
  10. use std::process::Command;
  11. use std::time::{UNIX_EPOCH, SystemTime};
  12. mod int128;
  13. mod spotify_id;
  14. mod arcvec;
  15. mod subfile;
  16. pub use util::int128::u128;
  17. pub use util::spotify_id::{SpotifyId, FileId};
  18. pub use util::arcvec::ArcVec;
  19. pub use util::subfile::Subfile;
  20. pub fn rand_vec<G: Rng, R: Rand>(rng: &mut G, size: usize) -> Vec<R> {
  21. rng.gen_iter().take(size).collect()
  22. }
  23. pub fn now_ms() -> i64 {
  24. let dur = match SystemTime::now().duration_since(UNIX_EPOCH) {
  25. Ok(dur) => dur,
  26. Err(err) => err.duration(),
  27. };
  28. (dur.as_secs() * 1000 + (dur.subsec_nanos() / 1000_000) as u64) as i64
  29. }
  30. pub fn mkdir_existing(path: &Path) -> io::Result<()> {
  31. fs::create_dir(path).or_else(|err| {
  32. if err.kind() == io::ErrorKind::AlreadyExists {
  33. Ok(())
  34. } else {
  35. Err(err)
  36. }
  37. })
  38. }
  39. pub fn run_program(program: &String) {
  40. info!("Running {}", program);
  41. let mut v: Vec<&str> = program.split_whitespace().collect();
  42. let status = Command::new(&v.remove(0))
  43. .args(&v)
  44. .status()
  45. .expect("program failed to start");
  46. info!("Exit status: {}", status);
  47. }
  48. pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint {
  49. let mut base = base.clone();
  50. let mut exp = exp.clone();
  51. let mut result: BigUint = One::one();
  52. while !exp.is_zero() {
  53. if exp.is_odd() {
  54. result = result.mul(&base).rem(modulus);
  55. }
  56. exp = exp.shr(1);
  57. base = (&base).mul(&base).rem(modulus);
  58. }
  59. result
  60. }
  61. pub struct StrChunks<'s>(&'s str, usize);
  62. pub trait StrChunksExt {
  63. fn chunks(&self, size: usize) -> StrChunks;
  64. }
  65. impl StrChunksExt for str {
  66. fn chunks(&self, size: usize) -> StrChunks {
  67. StrChunks(self, size)
  68. }
  69. }
  70. impl<'s> Iterator for StrChunks<'s> {
  71. type Item = &'s str;
  72. fn next(&mut self) -> Option<&'s str> {
  73. let &mut StrChunks(data, size) = self;
  74. if data.is_empty() {
  75. None
  76. } else {
  77. let ret = Some(&data[..size]);
  78. self.0 = &data[size..];
  79. ret
  80. }
  81. }
  82. }
  83. pub trait ReadSeek : ::std::io::Read + ::std::io::Seek { }
  84. impl <T: ::std::io::Read + ::std::io::Seek> ReadSeek for T { }
  85. pub trait Seq {
  86. fn next(&self) -> Self;
  87. }
  88. macro_rules! impl_seq {
  89. ($($ty:ty)*) => { $(
  90. impl Seq for $ty {
  91. fn next(&self) -> Self { *self + 1 }
  92. }
  93. )* }
  94. }
  95. impl_seq!(u8 u16 u32 u64 usize);
  96. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
  97. pub struct SeqGenerator<T: Seq>(T);
  98. impl <T: Seq> SeqGenerator<T> {
  99. pub fn new(value: T) -> Self {
  100. SeqGenerator(value)
  101. }
  102. pub fn get(&mut self) -> T {
  103. let value = self.0.next();
  104. mem::replace(&mut self.0, value)
  105. }
  106. }