split string utils into unit
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
52d470058a
commit
af81baae44
3 changed files with 33 additions and 25 deletions
22
src/core/utils/string.rs
Normal file
22
src/core/utils/string.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use rand::prelude::*;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn random_string(length: usize) -> String {
|
||||
thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
.take(length)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Parses the bytes into a string.
|
||||
#[inline]
|
||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
|
||||
let str: &str = str_from_bytes(bytes)?;
|
||||
Ok(str.to_owned())
|
||||
}
|
||||
|
||||
/// Parses the bytes into a string.
|
||||
#[inline]
|
||||
pub fn str_from_bytes(bytes: &[u8]) -> Result<&str> { Ok(std::str::from_utf8(bytes)?) }
|
Loading…
Add table
Add a link
Reference in a new issue