Skip to content

Commit

Permalink
Simplify Marker match by reusing discriminant
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Apr 17, 2024
1 parent 926682d commit f9f02d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rmp/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rmp"
version = "0.8.13"
version = "0.8.14"
authors = ["Evgeny Safronov <division494@gmail.com>"]
license = "MIT"
description = "Pure Rust MessagePack serialization implementation"
Expand Down
53 changes: 29 additions & 24 deletions rmp/src/marker.rs
Expand Up @@ -4,12 +4,26 @@ const FIXMAP_SIZE : u8 = 0x0f;

/// Format markers.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum Marker {
FixPos(u8),
FixNeg(i8),
Null,
True,
FixPos(u8) = 0x00,
FixNeg(i8) = 0xe0,
FixMap(u8) = 0x80,
FixArray(u8) = 0x90,
FixStr(u8) = 0xa0,
Null = 0xc0,
// Marked in MessagePack spec as never used.
Reserved,
False,
True,
Bin8,
Bin16,
Bin32,
Ext8,
Ext16,
Ext32,
F32,
F64,
U8,
U16,
U32,
Expand All @@ -18,35 +32,24 @@ pub enum Marker {
I16,
I32,
I64,
F32,
F64,
FixStr(u8),
FixExt1,
FixExt2,
FixExt4,
FixExt8,
FixExt16,
Str8,
Str16,
Str32,
Bin8,
Bin16,
Bin32,
FixArray(u8),
Array16,
Array32,
FixMap(u8),
Map16,
Map32,
FixExt1,
FixExt2,
FixExt4,
FixExt8,
FixExt16,
Ext8,
Ext16,
Ext32,
Reserved,
}

impl Marker {
/// Construct a msgpack marker from a single byte.
#[must_use]
#[inline]
pub fn from_u8(n: u8) -> Marker {
match n {
0x00 ..= 0x7f => Marker::FixPos(n),
Expand Down Expand Up @@ -91,7 +94,9 @@ impl Marker {
}

/// Converts a marker object into a single-byte representation.
#[must_use] pub fn to_u8(&self) -> u8 {
#[must_use]
#[inline]
pub fn to_u8(&self) -> u8 {
match *self {
Marker::FixPos(val) => val,
Marker::FixNeg(val) => val as u8,
Expand Down Expand Up @@ -146,14 +151,14 @@ impl Marker {
}

impl From<u8> for Marker {
#[inline]
#[inline(always)]
fn from(val: u8) -> Marker {
Marker::from_u8(val)
}
}

impl From<Marker> for u8 {
#[inline]
#[inline(always)]
fn from(val: Marker) -> Self {
val.to_u8()
}
Expand Down

0 comments on commit f9f02d8

Please sign in to comment.