aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2023-01-03 15:27:36 +0100
committerAlex Auvolat <alex@adnab.me>2023-01-03 15:27:36 +0100
commit8d5505514f950dc1ca1249a3385c9913b5b5e8e0 (patch)
treea74c043fe76deab6cb0919c6f002f0a4e87be19d /src/util
parent426d8784dac0e39879af52d980887d3692fc907c (diff)
downloadgarage-8d5505514f950dc1ca1249a3385c9913b5b5e8e0.tar.gz
garage-8d5505514f950dc1ca1249a3385c9913b5b5e8e0.zip
Make it explicit when using nonversioned encoding
Diffstat (limited to 'src/util')
-rw-r--r--src/util/data.rs15
-rw-r--r--src/util/encode.rs26
-rw-r--r--src/util/lib.rs1
-rw-r--r--src/util/migrate.rs15
4 files changed, 34 insertions, 23 deletions
diff --git a/src/util/data.rs b/src/util/data.rs
index 7715c2cc..b2a52e25 100644
--- a/src/util/data.rs
+++ b/src/util/data.rs
@@ -141,21 +141,6 @@ pub fn gen_uuid() -> Uuid {
rand::thread_rng().gen::<[u8; 32]>().into()
}
-// RMP serialization with names of fields and variants
-
-/// Serialize to MessagePack
-pub fn rmp_to_vec_all_named<T>(val: &T) -> Result<Vec<u8>, rmp_serde::encode::Error>
-where
- T: Serialize + ?Sized,
-{
- let mut wr = Vec::with_capacity(128);
- let mut se = rmp_serde::Serializer::new(&mut wr)
- .with_struct_map()
- .with_string_variants();
- val.serialize(&mut se)?;
- Ok(wr)
-}
-
/// Serialize to JSON, truncating long result
pub fn debug_serialize<T: Serialize>(x: T) -> String {
match serde_json::to_string(&x) {
diff --git a/src/util/encode.rs b/src/util/encode.rs
new file mode 100644
index 00000000..724e482a
--- /dev/null
+++ b/src/util/encode.rs
@@ -0,0 +1,26 @@
+use serde::{Deserialize, Serialize};
+
+/// Serialize to MessagePacki, without versionning
+/// (see garage_util::migrate for functions that manage versionned
+/// data formats)
+pub fn nonversioned_encode<T>(val: &T) -> Result<Vec<u8>, rmp_serde::encode::Error>
+where
+ T: Serialize + ?Sized,
+{
+ let mut wr = Vec::with_capacity(128);
+ let mut se = rmp_serde::Serializer::new(&mut wr)
+ .with_struct_map()
+ .with_string_variants();
+ val.serialize(&mut se)?;
+ Ok(wr)
+}
+
+/// Deserialize from MessagePacki, without versionning
+/// (see garage_util::migrate for functions that manage versionned
+/// data formats)
+pub fn nonversioned_decode<T>(bytes: &[u8]) -> Result<T, rmp_serde::decode::Error>
+where
+ T: for<'de> Deserialize<'de> + ?Sized,
+{
+ rmp_serde::decode::from_read_ref::<_, T>(bytes)
+}
diff --git a/src/util/lib.rs b/src/util/lib.rs
index fd3d5c7b..be82061f 100644
--- a/src/util/lib.rs
+++ b/src/util/lib.rs
@@ -8,6 +8,7 @@ pub mod background;
pub mod config;
pub mod crdt;
pub mod data;
+pub mod encode;
pub mod error;
pub mod formater;
pub mod metrics;
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;