diff options
author | Alex Auvolat <alex@adnab.me> | 2023-01-03 15:27:36 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-01-03 15:27:36 +0100 |
commit | 8d5505514f950dc1ca1249a3385c9913b5b5e8e0 (patch) | |
tree | a74c043fe76deab6cb0919c6f002f0a4e87be19d /src/util/migrate.rs | |
parent | 426d8784dac0e39879af52d980887d3692fc907c (diff) | |
download | garage-8d5505514f950dc1ca1249a3385c9913b5b5e8e0.tar.gz garage-8d5505514f950dc1ca1249a3385c9913b5b5e8e0.zip |
Make it explicit when using nonversioned encoding
Diffstat (limited to 'src/util/migrate.rs')
-rw-r--r-- | src/util/migrate.rs | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/util/migrate.rs b/src/util/migrate.rs index 199c68f6..f6028bf4 100644 --- a/src/util/migrate.rs +++ b/src/util/migrate.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static { /// A sequence of bytes to add at the beginning of the serialized /// string, to identify that the data is of this version. - const MARKER: &'static [u8] = b""; + const VERSION_MARKER: &'static [u8] = b""; /// The previous version of this data type, from which items of this version /// can be migrated. Set `type Previous = NoPrevious` to indicate that this datatype @@ -15,10 +15,9 @@ pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static { fn migrate(previous: Self::Previous) -> Self; fn decode(bytes: &[u8]) -> Option<Self> { - if bytes.len() >= Self::MARKER.len() && &bytes[..Self::MARKER.len()] == Self::MARKER { - if let Ok(value) = - rmp_serde::decode::from_read_ref::<_, Self>(&bytes[Self::MARKER.len()..]) - { + let marker_len = Self::VERSION_MARKER.len(); + if bytes.len() >= marker_len && &bytes[..marker_len] == Self::VERSION_MARKER { + if let Ok(value) = rmp_serde::decode::from_read_ref::<_, Self>(&bytes[marker_len..]) { return Some(value); } } @@ -28,7 +27,7 @@ pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static { fn encode(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> { let mut wr = Vec::with_capacity(128); - wr.extend_from_slice(Self::MARKER); + wr.extend_from_slice(Self::VERSION_MARKER); let mut se = rmp_serde::Serializer::new(&mut wr) .with_struct_map() .with_string_variants(); @@ -40,13 +39,13 @@ pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static { pub trait InitialFormat: Serialize + for<'de> Deserialize<'de> + 'static { /// A sequence of bytes to add at the beginning of the serialized /// string, to identify that the data is of this version. - const MARKER: &'static [u8] = b""; + const VERSION_MARKER: &'static [u8] = b""; } // ---- impl<T: InitialFormat> Migrate for T { - const MARKER: &'static [u8] = <T as InitialFormat>::MARKER; + const VERSION_MARKER: &'static [u8] = <T as InitialFormat>::VERSION_MARKER; type Previous = NoPrevious; |