Skip to content

Commit

Permalink
feat(util): add random_string/random_num
Browse files Browse the repository at this point in the history
  • Loading branch information
Huo Linhe committed Mar 4, 2016
1 parent d1ccad0 commit 66ad1cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ impl Drop for UnQlite {
}

pub use self::config::*;
pub use self::util::*;

mod openmode;
mod config;
mod util;

#[cfg(test)]
#[cfg(feature = "enable-threads")]
Expand Down
24 changes: 24 additions & 0 deletions src/engine/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::UnQlite;
use ffi::{unqlite_util_random_num, unqlite_util_random_string};

impl <'util> UnQlite {
/// Generate random string using the UnQLite PRNG.
///
/// It will generate a english alphabet based string of length buf_size (last argument).
fn random_string(&self, buf_size: u32) -> Vec<u8> {
unsafe {
let mut vec: Vec<u8> = Vec::with_capacity(buf_size as usize);
error_or!(unqlite_util_random_string(self.db, vec.as_mut_ptr() as *mut i8, buf_size)).unwrap();
vec
}
}

/// Generate random number using the UnQLite PRNG.
///
/// It will return a 32-bit unsigned integer between 0 and 0xFFFFFFFF.
fn random_num(&self) -> u32 {
unsafe {
unqlite_util_random_num(self.db)
}
}
}

0 comments on commit 66ad1cd

Please sign in to comment.