pub struct Nanoid<const N: usize = 21, A: Alphabet = Base64UrlAlphabet> { /* private fields */ }Expand description
A Nano ID.
§Generic parameters
N: The length of the Nano ID. The default is21.A: The alphabet used in the Nano ID. The default isBase64UrlAlphabet.
§Generating
When you want a new Nano ID, you can generate one using the Nanoid::new.
use nid::Nanoid;
let id: Nanoid = Nanoid::new();§Parsing
You can parse a string into a Nano ID using Nanoid::try_from_str, std::str::FromStr or TryFrom<String>.
use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_str("K8N4Q7MNmeHJ-OHHoVDcz")?;
let id: Nanoid = "3hYR3muA_xvjMrrrqFWxF".parse()?;
let id: Nanoid = "iH26rJ8CpRz-gfIh7TSRu".to_string().try_into()?;If you try to parse an invalid Nano ID, you will get an error.
use nid::{Nanoid, ParseError};
let result: Result<Nanoid, _> = "61psxw-too_short".parse();
assert!(matches!(result, Err(ParseError::InvalidLength { .. })));
let result: Result<Nanoid, _> = "6yt_invalid_char#####".to_string().try_into();
assert!(matches!(result, Err(ParseError::InvalidCharacter(_))));§Converting to a string
You can get the string representation of the Nano ID using Nanoid::as_str, AsRef<str> or Display.
use nid::Nanoid;
let id: Nanoid = "Z9ifKfmBL7j69naN7hthu".parse()?;
// Convert to &str
assert_eq!(id.as_str(), "Z9ifKfmBL7j69naN7hthu");
assert_eq!(id.as_ref(), "Z9ifKfmBL7j69naN7hthu");
// Convert to String
assert_eq!(id.to_string(), "Z9ifKfmBL7j69naN7hthu");§Examples
use nid::{alphabet::Base62Alphabet, Nanoid};
// Generate a new Nano ID and print it.
let id: Nanoid = Nanoid::new();
println!("{}", id);
// Parse a string into a Nano ID and convert it back to a string.
let id: Nanoid = "abcdefg1234567UVWXYZ_".parse()?;
let s = id.to_string();
// Parse a string into a Nano ID with a different length and alphabet.
let id: Nanoid<9, Base62Alphabet> = "abc123XYZ".parse()?;Implementations§
Source§impl<const N: usize, A: Alphabet> Nanoid<N, A>
impl<const N: usize, A: Alphabet> Nanoid<N, A>
Sourcepub fn new() -> Self
pub fn new() -> Self
Generate a new Nano ID using random number generator seeded by the system.
§Panics
The function will panic if the random number generator is not able to generate random numbers.
This function also panics if the provided Alphabet produces non-ascii characters, but this
never happens unless the alphabet is implemented incorrectly.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::new();Sourcepub fn new_with(rng: impl Rng) -> Self
pub fn new_with(rng: impl Rng) -> Self
Generate a new Nano ID using the provided random number generator.
§Panics
The function will panic if the provided random number generator is not able to generate random numbers.
This function also panics if the provided Alphabet produces non-ascii characters, but this
never happens unless the alphabet is implemented incorrectly.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::new_with(rand::thread_rng());Sourcepub const fn try_from_str(s: &str) -> Result<Self, ParseError>
pub const fn try_from_str(s: &str) -> Result<Self, ParseError>
Parse a string into a Nanoid.
§Errors
- If the length of the string is not equal to the expected length, this method returns
ParseError::InvalidLength. - If the string contains a character that is not in the alphabet, this method returns
ParseError::InvalidCharacter.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_str("r9p_QLd_9CD63JqQaGQ9I")?;Sourcepub const fn try_from_bytes(buf: &[u8; N]) -> Result<Self, ParseError>
pub const fn try_from_bytes(buf: &[u8; N]) -> Result<Self, ParseError>
Parse a byte array into a Nanoid.
§Errors
If the byte array contains a character that is not in the alphabet, this method returns ParseError::InvalidCharacter.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_bytes(b"0tY_GxufiwmAxvmHR7G0R")?;Trait Implementations§
Source§impl<const N: usize, A: Alphabet> Archive for Nanoid<N, A>
Available on crate feature rkyv only.
impl<const N: usize, A: Alphabet> Archive for Nanoid<N, A>
rkyv only.Source§type Resolver = [(); N]
type Resolver = [(); N]
Source§fn resolve(&self, _: Self::Resolver, out: Place<Self::Archived>)
fn resolve(&self, _: Self::Resolver, out: Place<Self::Archived>)
Source§const COPY_OPTIMIZATION: CopyOptimization<Self> = _
const COPY_OPTIMIZATION: CopyOptimization<Self> = _
serialize. Read moreSource§impl<'de, const N: usize, A: Alphabet> Deserialize<'de> for Nanoid<N, A>
Available on crate feature serde only.
impl<'de, const N: usize, A: Alphabet> Deserialize<'de> for Nanoid<N, A>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Nanoid<N, A>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Nanoid<N, A>, D::Error>where
D: Deserializer<'de>,
Source§impl<const N: usize, A: Alphabet, D: Fallible + ?Sized> Deserialize<Nanoid<N, A>, D> for [u8; N]
Available on crate feature rkyv only.
impl<const N: usize, A: Alphabet, D: Fallible + ?Sized> Deserialize<Nanoid<N, A>, D> for [u8; N]
rkyv only.Source§impl<const N: usize, A: Alphabet> Ord for Nanoid<N, A>
impl<const N: usize, A: Alphabet> Ord for Nanoid<N, A>
Source§impl<const N: usize, A: Alphabet> PartialOrd for Nanoid<N, A>
impl<const N: usize, A: Alphabet> PartialOrd for Nanoid<N, A>
Source§impl<const N: usize, A: Alphabet, S> Serialize<S> for Nanoid<N, A>
Available on crate feature rkyv only.
impl<const N: usize, A: Alphabet, S> Serialize<S> for Nanoid<N, A>
rkyv only.Source§impl<const N: usize, A: Alphabet> Serialize for Nanoid<N, A>
Available on crate feature serde only.
impl<const N: usize, A: Alphabet> Serialize for Nanoid<N, A>
serde only.impl<const N: usize, A: Alphabet> Copy for Nanoid<N, A>
impl<const N: usize, A: Alphabet> Eq for Nanoid<N, A>
Auto Trait Implementations§
impl<const N: usize, A> Freeze for Nanoid<N, A>
impl<const N: usize, A> RefUnwindSafe for Nanoid<N, A>
impl<const N: usize, A> Send for Nanoid<N, A>
impl<const N: usize, A> Sync for Nanoid<N, A>
impl<const N: usize, A> Unpin for Nanoid<N, A>
impl<const N: usize, A> UnsafeUnpin for Nanoid<N, A>
impl<const N: usize, A> UnwindSafe for Nanoid<N, A>
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.