Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Created March 21, 2022 02:49
Show Gist options
  • Select an option

  • Save jayhuang75/f839e8dac38919800cc13b0659341bda to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/f839e8dac38919800cc13b0659341bda to your computer and use it in GitHub Desktop.
url_shortener
#[derive(Debug, Clone)]
pub struct Shortener {
id: u64,
generator: Harsh,
}
impl Default for Shortener {
fn default() -> Self {
Shortener{
id: 0,
generator: Harsh::default(),
}
}
}
impl Shortener {
fn next_id(&mut self) -> String {
let hashed = self.generator.encode(&[self.id]);
self.id += 1;
hashed
}
}
#[derive(Debug)]
pub struct TinyUrl {
pub shortener: Shortener,
}
impl TinyUrl {
pub fn new() -> TinyUrl {
TinyUrl {
shortener: Shortener::default(),
}
}
pub fn set(&mut self, url: &str) -> String {
self.shortener.next_id()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment