From 784cb4c62674f6c2a189d487175647d2dce04765 Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Mon, 25 Nov 2024 12:52:45 +0100 Subject: [PATCH] fix: apply PR feedbacks --- .../src/unicast/establishment/ext/patch.rs | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/io/zenoh-transport/src/unicast/establishment/ext/patch.rs b/io/zenoh-transport/src/unicast/establishment/ext/patch.rs index acca883912..d9d91caef8 100644 --- a/io/zenoh-transport/src/unicast/establishment/ext/patch.rs +++ b/io/zenoh-transport/src/unicast/establishment/ext/patch.rs @@ -11,7 +11,7 @@ // Contributors: // ZettaScale Zenoh Team, // -use core::marker::PhantomData; +use std::{cmp::min, marker::PhantomData}; use async_trait::async_trait; use zenoh_buffers::{ @@ -19,8 +19,8 @@ use zenoh_buffers::{ writer::{DidntWrite, Writer}, }; use zenoh_codec::{RCodec, WCodec, Zenoh080}; -use zenoh_protocol::transport::init; -use zenoh_result::Error as ZError; +use zenoh_protocol::transport::{init, init::ext::PatchType}; +use zenoh_result::{bail, Error as ZError}; use crate::unicast::establishment::{AcceptFsm, OpenFsm}; @@ -40,17 +40,17 @@ impl<'a> PatchFsm<'a> { /*************************************/ #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct StateOpen { - patch: init::ext::PatchType, + patch: PatchType, } impl StateOpen { pub(crate) const fn new() -> Self { Self { - patch: init::ext::PatchType::NONE, + patch: PatchType::NONE, } } - pub(crate) const fn get(&self) -> init::ext::PatchType { + pub(crate) const fn get(&self) -> PatchType { self.patch } } @@ -60,21 +60,27 @@ impl<'a> OpenFsm for &'a PatchFsm<'a> { type Error = ZError; type SendInitSynIn = &'a StateOpen; - type SendInitSynOut = init::ext::PatchType; + type SendInitSynOut = PatchType; async fn send_init_syn( self, _state: Self::SendInitSynIn, ) -> Result { - Ok(init::ext::PatchType::CURRENT) + Ok(PatchType::CURRENT) } - type RecvInitAckIn = (&'a mut StateOpen, init::ext::PatchType); + type RecvInitAckIn = (&'a mut StateOpen, PatchType); type RecvInitAckOut = (); async fn recv_init_ack( self, input: Self::RecvInitAckIn, ) -> Result { let (state, other_ext) = input; + if other_ext > PatchType::CURRENT { + bail!( + "Acceptor patch should be lesser or equal to {}, found {other_ext}", + PatchType::CURRENT + ); + } state.patch = other_ext; Ok(()) } @@ -85,7 +91,7 @@ impl<'a> OpenFsm for &'a PatchFsm<'a> { self, _state: Self::SendOpenSynIn, ) -> Result { - unimplemented!() + unimplemented!("There is no patch extension in OPEN") } type RecvOpenAckIn = (&'a mut StateOpen, ()); @@ -94,7 +100,7 @@ impl<'a> OpenFsm for &'a PatchFsm<'a> { self, _state: Self::RecvOpenAckIn, ) -> Result { - unimplemented!() + unimplemented!("There is no patch extension in OPEN") } } @@ -103,24 +109,24 @@ impl<'a> OpenFsm for &'a PatchFsm<'a> { /*************************************/ #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct StateAccept { - patch: init::ext::PatchType, + patch: PatchType, } impl StateAccept { pub(crate) const fn new() -> Self { Self { - patch: init::ext::PatchType::NONE, + patch: PatchType::NONE, } } - pub(crate) const fn get(&self) -> init::ext::PatchType { + pub(crate) const fn get(&self) -> PatchType { self.patch } #[cfg(test)] pub(crate) fn rand() -> Self { Self { - patch: init::ext::PatchType::rand(), + patch: PatchType::rand(), } } } @@ -147,7 +153,7 @@ where fn read(self, reader: &mut R) -> Result { let raw: u8 = self.read(&mut *reader)?; - let patch = init::ext::PatchType::new(raw); + let patch = PatchType::new(raw); Ok(StateAccept { patch }) } } @@ -156,7 +162,7 @@ where impl<'a> AcceptFsm for &'a PatchFsm<'a> { type Error = ZError; - type RecvInitSynIn = (&'a mut StateAccept, init::ext::PatchType); + type RecvInitSynIn = (&'a mut StateAccept, PatchType); type RecvInitSynOut = (); async fn recv_init_syn( self, @@ -168,12 +174,12 @@ impl<'a> AcceptFsm for &'a PatchFsm<'a> { } type SendInitAckIn = &'a StateAccept; - type SendInitAckOut = init::ext::PatchType; + type SendInitAckOut = PatchType; async fn send_init_ack( self, - _state: Self::SendInitAckIn, + state: Self::SendInitAckIn, ) -> Result { - Ok(init::ext::PatchType::CURRENT) + Ok(min(PatchType::CURRENT, state.patch)) } type RecvOpenSynIn = (&'a mut StateAccept, ()); @@ -182,7 +188,7 @@ impl<'a> AcceptFsm for &'a PatchFsm<'a> { self, _state: Self::RecvOpenSynIn, ) -> Result { - unimplemented!() + unimplemented!("There is no patch extension in OPEN") } type SendOpenAckIn = &'a StateAccept; @@ -191,6 +197,6 @@ impl<'a> AcceptFsm for &'a PatchFsm<'a> { self, _state: Self::SendOpenAckIn, ) -> Result { - unimplemented!() + unimplemented!("There is no patch extension in OPEN") } }