Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 22, 2019 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/ce1e896c4a1139cbf88ab1f9e437e1d5 to your computer and use it in GitHub Desktop.
Save rust-play/ce1e896c4a1139cbf88ab1f9e437e1d5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
pub type Payload<R> = (Role<R>, Target);
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SGSecret(String);
impl Default for SGSecret {
fn default() -> Self{ Self(String::default()) }
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum Role<R> {
User,
CustomRole(R),
}
impl<R> Default for Role<R> {
fn default() -> Self{ Role::User }
}
#[derive(Debug, Serialize, Deserialize)]
pub enum Target {
Global,
CustomTarget(String),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthPayload<R> {
role: Role<R>,
random_key: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthEngine<R> {
bearer: SGSecret,
payload: AuthPayload<R>,
}
impl<R> for AuthEngine<R> {
pub fn rm<'d>(self, raw_key: String) -> Option<Payload> where R: serde::Deserialize<'d> {
let dual = raw_key.split(":::").collect::<Vec<&str>>();
let key = bincode::serialize(dual[0])?;
let check_key = db.remove(key)?; // Remove key from DB
if let Some(binary) = check_key {
let data = bincode::deserialize::<AuthPayload<R>>(&binary)?;
Some((data.role, data.target, data.random_key))
}else {
Ok(None)
}
}
}
// ERROR FROM RUSTC //
error[E0597]: `binary` does not live long enough
--> src/secrets/secrets_engine.rs:244:63
|
231 | pub fn rm<'d>(self, raw_key: Secret<String>) -> Result<(custom_codes::DbOps, Option<FullPayload<R>>), SGError>
| -- lifetime `'d` defined here
...
244 | let data = bincode::deserialize::<AuthPayload<R>>(&binary)?;
| ---------------------------------------^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `binary` is borrowed for `'d`
245 | Ok((custom_codes::DbOps::Deleted, Some((data.role, data.lease, data.target, data.random_key))))
246 | }else {
| - `binary` dropped here while still borrowed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment