From 3c14ad1f4096c2b06313ac2c295ca56b256683fd Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Fri, 6 Sep 2024 13:02:45 -0300 Subject: [PATCH 1/2] feat(zenoh_id): exposing into slice & try from slice Allowing users to create a ZenohId from a slice, using TryFrom, and also allowing users to convert a ZenohId into a [u8; 16]. --- commons/zenoh-config/src/wrappers.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/commons/zenoh-config/src/wrappers.rs b/commons/zenoh-config/src/wrappers.rs index fd6d2ef50b..bdcea8a52f 100644 --- a/commons/zenoh-config/src/wrappers.rs +++ b/commons/zenoh-config/src/wrappers.rs @@ -54,6 +54,15 @@ impl From for ZenohId { } } +impl TryFrom<&[u8]> for ZenohId { + type Error = zenoh_result::Error; + + fn try_from(value: &[u8]) -> Result { + let proto: ZenohIdProto = value.try_into()?; + Ok(ZenohId::from(proto)) + } +} + impl From for ZenohIdProto { fn from(id: ZenohId) -> Self { id.0 @@ -72,6 +81,13 @@ impl From for OwnedKeyExpr { } } +impl From for [u8; ZenohIdProto::MAX_SIZE] { + fn from(value: ZenohId) -> Self { + let proto: ZenohIdProto = value.into(); + proto.to_le_bytes() + } +} + impl From<&ZenohId> for OwnedKeyExpr { fn from(zid: &ZenohId) -> Self { (*zid).into() From 5e0475923417acd7080746c2a5ddc8c4bd39c835 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Mon, 9 Sep 2024 10:41:23 -0300 Subject: [PATCH 2/2] feat(zenoh_id): replacing from slice with function --- commons/zenoh-config/src/wrappers.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/commons/zenoh-config/src/wrappers.rs b/commons/zenoh-config/src/wrappers.rs index bdcea8a52f..d04950f21e 100644 --- a/commons/zenoh-config/src/wrappers.rs +++ b/commons/zenoh-config/src/wrappers.rs @@ -35,6 +35,10 @@ impl ZenohId { pub fn into_keyexpr(self) -> OwnedKeyExpr { self.into() } + + pub fn to_le_bytes(self) -> [u8; uhlc::ID::MAX_SIZE] { + self.0.to_le_bytes() + } } impl fmt::Debug for ZenohId { @@ -81,13 +85,6 @@ impl From for OwnedKeyExpr { } } -impl From for [u8; ZenohIdProto::MAX_SIZE] { - fn from(value: ZenohId) -> Self { - let proto: ZenohIdProto = value.into(); - proto.to_le_bytes() - } -} - impl From<&ZenohId> for OwnedKeyExpr { fn from(zid: &ZenohId) -> Self { (*zid).into()