-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): add random_string/random_num
- Loading branch information
Huo Linhe
committed
Mar 4, 2016
1 parent
d1ccad0
commit 66ad1cd
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |