From a52625a071554f3bd3009c0a3a68442746171a5b Mon Sep 17 00:00:00 2001 From: Murph Murphy Date: Mon, 16 Dec 2019 14:35:53 -0700 Subject: [PATCH] fix(storage): storage clears all matching tokens Change Storage to clear all existing JSONTokens with the current `hash` on set if at least one exists. Fixes #117 if it's a real issue. --- src/storage.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index 1b710ef..950d015 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -3,6 +3,7 @@ // See project root for licensing information. // +use std::cmp::Ordering; use std::collections::hash_map::DefaultHasher; use std::error::Error; use std::fmt; @@ -98,8 +99,8 @@ impl TokenStorage for MemoryStorage { token: Option, ) -> Result<(), NullError> { let matched = self.tokens.iter().find_position(|x| x.hash == scope_hash); - if let Some((idx, _)) = matched { - self.tokens.remove(idx); + if let Some(_) = matched { + self.tokens.retain(|x| x.hash != scope_hash); } match token { @@ -144,6 +145,26 @@ struct JSONToken { pub token: Token, } +impl PartialEq for JSONToken { + fn eq(&self, other: &Self) -> bool { + self.hash == other.hash + } +} + +impl Eq for JSONToken {} + +impl PartialOrd for JSONToken { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for JSONToken { + fn cmp(&self, other: &Self) -> Ordering { + self.hash.cmp(&other.hash) + } +} + /// List of tokens in a JSON object #[derive(Serialize, Deserialize)] struct JSONTokens { @@ -232,8 +253,8 @@ impl TokenStorage for DiskTokenStorage { token: Option, ) -> Result<(), Self::Error> { let matched = self.tokens.iter().find_position(|x| x.hash == scope_hash); - if let Some((idx, _)) = matched { - self.tokens.remove(idx); + if let Some(_) = matched { + self.tokens.retain(|x| x.hash != scope_hash); } match token {