Skip to content

Commit

Permalink
fix: remove varint implementation (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo authored Oct 2, 2024
1 parent 25933b1 commit e1481cd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 74 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ pyo3 = { version = "0.21.2", features = [
] }
validated_struct = "2.1.0"
zenoh = { version = "1.0.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["unstable", "internal"], default-features = false }
zenoh-ext = { version = "1.0.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", default-features = false, optional = true }
zenoh-ext = { version = "1.0.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["internal"], optional = true }
9 changes: 1 addition & 8 deletions examples/z_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,12 @@ def main():

if True:
# Numeric: UInt8, UInt16, Uint32, UInt64, UInt128, Int8, Int16, Int32, Int64,
# Int128, Float32, Float64, float (handled as Float64)
# Int128, int (handled as int32), Float32, Float64, float (handled as Float64)
input = UInt32(1234)
payload = z_serialize(input)
output = z_deserialize(UInt32, payload)
assert input == output

# Varint LEB128: VarInt, VarUInt, int (handled as VarInt)
input = 42
payload = z_serialize(input)
assert len(payload.to_bytes()) == 1
output = z_deserialize(int, payload)
assert input == output

# list
input = [0.0, 1.5, 42.0] # all items must have the same type
payload = z_serialize(input)
Expand Down
20 changes: 2 additions & 18 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ enum SupportedType {
Float,
Float32,
Float64,
VarInt,
VarUInt,
Bool,
List,
Tuple,
Expand Down Expand Up @@ -75,8 +73,6 @@ impl SupportedType {
add_type::<PyFloat>(py, &dict, SupportedType::Float);
add_wrapper_type("Float32", SupportedType::Float32);
add_wrapper_type("Float64", SupportedType::Float64);
add_wrapper_type("VarInt", SupportedType::VarInt);
add_wrapper_type("VarUInt", SupportedType::VarUInt);
add_type::<PyBool>(py, &dict, SupportedType::Bool);
add_type::<PyList>(py, &dict, SupportedType::List);
add_type::<PyTuple>(py, &dict, SupportedType::Tuple);
Expand Down Expand Up @@ -106,8 +102,6 @@ impl SupportedType {
n if n == Self::Float as u8 => Self::Float,
n if n == Self::Float32 as u8 => Self::Float32,
n if n == Self::Float64 as u8 => Self::Float64,
n if n == Self::VarInt as u8 => Self::VarInt,
n if n == Self::VarUInt as u8 => Self::VarUInt,
n if n == Self::Bool as u8 => Self::Bool,
n if n == Self::List as u8 => Self::List,
n if n == Self::Tuple as u8 => Self::Tuple,
Expand Down Expand Up @@ -173,7 +167,7 @@ fn serialize_impl(
SupportedType::Str => serializer.serialize(&obj.downcast::<PyString>()?.to_cow()?),
SupportedType::Int8 => serializer.serialize(i8::extract_bound(obj)?),
SupportedType::Int16 => serializer.serialize(i16::extract_bound(obj)?),
SupportedType::Int32 => serializer.serialize(i32::extract_bound(obj)?),
SupportedType::Int | SupportedType::Int32 => serializer.serialize(i32::extract_bound(obj)?),
SupportedType::Int64 => serializer.serialize(i64::extract_bound(obj)?),
SupportedType::Int128 => serializer.serialize(i128::extract_bound(obj)?),
SupportedType::UInt8 => serializer.serialize(u8::extract_bound(obj)?),
Expand All @@ -185,10 +179,6 @@ fn serialize_impl(
serializer.serialize(f64::extract_bound(obj)?)
}
SupportedType::Float32 => serializer.serialize(f64::extract_bound(obj)? as f32),
SupportedType::VarInt | SupportedType::Int => {
serializer.serialize(VarInt(i64::extract_bound(obj)?))
}
SupportedType::VarUInt => serializer.serialize(VarInt(u64::extract_bound(obj)?)),
SupportedType::Bool => serializer.serialize(bool::extract_bound(obj)?),
SupportedType::List => serialize_iter(
serializer,
Expand Down Expand Up @@ -327,7 +317,7 @@ fn deserialize_impl(
PyBytes::new_bound(py, &deserializer.deserialize::<Vec<u8>>()?).into_py(py)
}
SupportedType::Str => deserializer.deserialize::<String>()?.into_py(py),
SupportedType::Int => deserializer.deserialize::<VarInt<i64>>()?.0.into_py(py),
SupportedType::Int => deserializer.deserialize::<i32>()?.into_py(py),
SupportedType::Int8 => deserialize_wrapper!(i8, Int8),
SupportedType::Int16 => deserialize_wrapper!(i16, Int16),
SupportedType::Int32 => deserialize_wrapper!(i32, Int32),
Expand All @@ -341,12 +331,6 @@ fn deserialize_impl(
SupportedType::Float => deserializer.deserialize::<f64>()?.into_py(py),
SupportedType::Float32 => deserialize_wrapper!(f32, Float32),
SupportedType::Float64 => deserialize_wrapper!(f64, Float64),
SupportedType::VarInt => import!(py, "zenoh.ext", VarInt)
.call1((deserializer.deserialize::<VarInt<i64>>()?.0,))?
.into_py(py),
SupportedType::VarUInt => import!(py, "zenoh.ext", VarUInt)
.call1((deserializer.deserialize::<VarInt<u64>>()?.0,))?
.into_py(py),
SupportedType::Bool => deserializer.deserialize::<bool>()?.into_py(py),
tp @ (SupportedType::List | SupportedType::Set | SupportedType::FrozenSet) => {
deserialize_collection(deserializer, py, tp, unwrap_args()?)?
Expand Down
12 changes: 5 additions & 7 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
UInt32,
UInt64,
UInt128,
VarInt,
VarUInt,
z_deserialize,
z_serialize,
)
Expand All @@ -41,12 +39,12 @@
(bytes, b"foo"),
(bytearray, bytearray(b"foo")),
(str, "foo"),
(int, -42),
(int, 42),
*((tp, tp(-42)) for tp in (Int8, Int16, Int32, Int64, Int128)),
*(
(tp, tp(i))
for i in (-42, 42)
for tp in (int, Int8, Int16, Int32, Int64, Int128)
),
*((tp, tp(42)) for tp in (UInt8, UInt16, UInt32, UInt64, UInt128)),
(VarUInt, VarUInt(42)),
(VarInt, VarInt(-42)),
(float, 0.5),
(Float64, Float64(0.5)),
(Float32, Float32(0.5)),
Expand Down
8 changes: 0 additions & 8 deletions zenoh/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,3 @@ class Float32(float):

class Float64(float):
"""float subclass enabling to (de)serialize 64bit floating point numbers."""


class VarUInt(int):
"""int subclass enabling to (de)serialize unsigned varint."""


class VarInt(int):
"""int subclass enabling to (de)serialize signed varint."""
6 changes: 0 additions & 6 deletions zenoh/ext.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class Float32(float):
class Float64(float):
"""float subclass enabling to (de)serialize 64bit floating point numbers."""

class VarUInt(int):
"""int subclass enabling to (de)serialize unsigned varint."""

class VarInt(int):
"""int subclass enabling to (de)serialize signed varint."""

class ZDeserializeError(Exception):
pass

Expand Down

0 comments on commit e1481cd

Please sign in to comment.