From bb742056f86c6fa39df1b6c3e9ea6eeeffee07d5 Mon Sep 17 00:00:00 2001 From: Leo Unoki Date: Wed, 18 Oct 2023 08:34:49 +0100 Subject: [PATCH] Remove dyn Any methods from Value --- jstz_core/src/kv/transaction.rs | 4 +-- jstz_core/src/kv/value.rs | 46 +++++---------------------------- 2 files changed, 8 insertions(+), 42 deletions(-) diff --git a/jstz_core/src/kv/transaction.rs b/jstz_core/src/kv/transaction.rs index cb5c2398d..d8378b2bf 100644 --- a/jstz_core/src/kv/transaction.rs +++ b/jstz_core/src/kv/transaction.rs @@ -85,7 +85,7 @@ impl SnapshotEntry { where V: Value, { - self.value.downcast_ref().unwrap() + self.value.as_any().downcast_ref().unwrap() } fn as_mut(&mut self) -> &mut V @@ -93,7 +93,7 @@ impl SnapshotEntry { V: Value, { self.dirty = true; - self.value.downcast_mut().unwrap() + self.value.as_any_mut().downcast_mut().unwrap() } fn into_value(self) -> V diff --git a/jstz_core/src/kv/value.rs b/jstz_core/src/kv/value.rs index e197f4b9c..dbc39ee49 100644 --- a/jstz_core/src/kv/value.rs +++ b/jstz_core/src/kv/value.rs @@ -1,4 +1,4 @@ -use std::any::{Any, TypeId}; +use std::any::Any; use std::fmt::Debug; use bincode::Options; @@ -35,7 +35,10 @@ pub trait Value: Any + Debug + erased_serde::Serialize { fn as_any_mut(&mut self) -> &mut dyn Any; } -impl Value for T where T: Any + Debug + erased_serde::Serialize { +impl Value for T +where + T: Any + Debug + erased_serde::Serialize, +{ fn as_any(&self) -> &dyn Any { self } @@ -43,43 +46,6 @@ impl Value for T where T: Any + Debug + erased_serde::Serialize { fn as_any_mut(&mut self) -> &mut dyn Any { self } - -// Since trait downcasting isn't permitted, we implement all methods -// from `dyn Any`. -impl dyn Value { - pub fn is(&self) -> bool { - let t = TypeId::of::(); - let concrete = self.type_id(); - t == concrete - } - - pub unsafe fn downcast_ref_unchecked(&self) -> &T { - unsafe { &*(self as *const dyn Value as *const T) } - } - - pub fn downcast_ref(&self) -> Option<&T> { - if self.is::() { - unsafe { Some(self.downcast_ref_unchecked()) } - } else { - None - } - } - - pub fn downcast_mut(&mut self) -> Option<&mut T> { - if self.is::() { - unsafe { Some(self.downcast_mut_unchecked()) } - } else { - None - } - } - - pub unsafe fn downcast_mut_unchecked(&mut self) -> &mut T { - unsafe { &mut *(self as *mut dyn Value as *mut T) } - } - - pub fn serialize(&self) -> Vec { - serialize(self) - } } #[derive(Debug, Deref, DerefMut)] @@ -99,7 +65,7 @@ impl BoxedValue { where T: Any, { - if self.is::() { + if self.as_any().is::() { Ok(unsafe { self.downcast_unchecked() }) } else { Err(self)