History

[transport] implement Transport for &mut T: Transport as well

Sebastian Thiel committed 5 years ago

Commit 372fb811 parent f2485574 files · +63 −8Browse files

Unified · Split

4 files
  1. git-protocol/tests/fetch/v2.rs +10 −8
  2. git-transport/src/client/async_io/traits.rs +16 −0
  3. git-transport/src/client/blocking_io/traits.rs +14 −0
  4. git-transport/src/client/traits.rs +23 −0
git-protocol/tests/fetch/v2.rs+10−8
@@ -58,13 +58,15 @@ async fn ref_in_want() -> crate::Result { · 57 unmodified lines
5858 want_refs: vec!["refs/heads/main".into()],
5959 ..CloneRefInWantDelegate::default()
6060 };
61− let (delegate, out) = git_protocol::fetch(
62− transport(
63− out,
64− "v2/clone-ref-in-want.response",
65− Protocol::V2,
66− git_transport::client::git::ConnectMode::Daemon,
67− ),
61+ let mut transport = transport(
62+ out,
63+ "v2/clone-ref-in-want.response",
64+ Protocol::V2,
65+ git_transport::client::git::ConnectMode::Daemon,
66+ );
67+
68+ let (delegate, _) = git_protocol::fetch(
69+ &mut transport,
6870 delegate,
6971 git_protocol::credentials::helper,
7072 progress::Discard,
@@ -81,7 +83,7 @@ async fn ref_in_want() -> crate::Result { · 10 unmodified lines
8183 );
8284 assert_eq!(delegate.pack_bytes, 641, "Should get packfile");
8385 assert_eq!(
84− out.into_inner().1.as_bstr(),
86+ transport.into_inner().1.as_bstr(),
8587 format!(
8688 "002fgit-upload-pack does/not/matter\0\0version=2\00012command=fetch
8789 001aagent={}
git-transport/src/client/async_io/traits.rs+16−0
@@ -61,6 +61,22 @@ impl<T: Transport + ?Sized> Transport for Box<T> { · 60 unmodified lines
6161 }
6262 }
6363
64+// Would be nice if the box implementation could auto-forward to all implemented traits.
65+#[async_trait(?Send)]
66+impl<T: Transport + ?Sized> Transport for &mut T {
67+ async fn handshake<'a>(
68+ &mut self,
69+ service: Service,
70+ extra_parameters: &'a [(&'a str, Option<&'a str>)],
71+ ) -> Result<SetServiceResponse<'_>, Error> {
72+ self.deref_mut().handshake(service, extra_parameters).await
73+ }
74+
75+ async fn close(&mut self) -> Result<(), Error> {
76+ self.deref_mut().close().await
77+ }
78+}
79+
6480 /// An extension trait to add more methods to everything implementing [`Transport`].
6581 #[async_trait(?Send)]
6682 pub trait TransportV2Ext {
git-transport/src/client/blocking_io/traits.rs+14−0
@@ -57,6 +57,20 @@ impl<T: Transport + ?Sized> Transport for Box<T> { · 56 unmodified lines
5757 }
5858 }
5959
60+impl<T: Transport + ?Sized> Transport for &mut T {
61+ fn handshake<'a>(
62+ &mut self,
63+ service: Service,
64+ extra_parameters: &'a [(&'a str, Option<&'a str>)],
65+ ) -> Result<SetServiceResponse<'_>, Error> {
66+ self.deref_mut().handshake(service, extra_parameters)
67+ }
68+
69+ fn close(&mut self) -> Result<(), Error> {
70+ self.deref_mut().close()
71+ }
72+}
73+
6074 /// An extension trait to add more methods to everything implementing [`Transport`].
6175 pub trait TransportV2Ext {
6276 /// Invoke a protocol V2 style `command` with given `capabilities` and optional command specific `arguments`.
git-transport/src/client/traits.rs+23−0
@@ -80,3 +80,26 @@ impl<T: TransportWithoutIO + ?Sized> TransportWithoutIO for Box<T> { · 79 unmodified lines
8080 self.deref().is_stateful()
8181 }
8282 }
83+
84+impl<T: TransportWithoutIO + ?Sized> TransportWithoutIO for &mut T {
85+ fn set_identity(&mut self, identity: Identity) -> Result<(), Error> {
86+ self.deref_mut().set_identity(identity)
87+ }
88+
89+ #[cfg(any(feature = "blocking-client", feature = "async-client"))]
90+ fn request(&mut self, write_mode: WriteMode, on_into_read: MessageKind) -> Result<RequestWriter<'_>, Error> {
91+ self.deref_mut().request(write_mode, on_into_read)
92+ }
93+
94+ fn to_url(&self) -> String {
95+ self.deref().to_url()
96+ }
97+
98+ fn supported_protocol_versions(&self) -> &[Protocol] {
99+ self.deref().supported_protocol_versions()
100+ }
101+
102+ fn is_stateful(&self) -> bool {
103+ self.deref().is_stateful()
104+ }
105+}