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
58 want_refs: vec!["refs/heads/main".into()],58 want_refs: vec!["refs/heads/main".into()],
59 ..CloneRefInWantDelegate::default()59 ..CloneRefInWantDelegate::default()
60 };60 };
61 let (delegate, out) = git_protocol::fetch(61 let mut transport = transport(
62 transport(62 out,
63 out,63 "v2/clone-ref-in-want.response",
64 "v2/clone-ref-in-want.response",64 Protocol::V2,
65 Protocol::V2,65 git_transport::client::git::ConnectMode::Daemon,
66 git_transport::client::git::ConnectMode::Daemon,66 );
67 ),67
68 let (delegate, _) = git_protocol::fetch(
69 &mut transport,
68 delegate,70 delegate,
69 git_protocol::credentials::helper,71 git_protocol::credentials::helper,
70 progress::Discard,72 progress::Discard,
@@ -81,7 +83,7 @@ async fn ref_in_want() -> crate::Result { · 10 unmodified lines
81 );83 );
82 assert_eq!(delegate.pack_bytes, 641, "Should get packfile");84 assert_eq!(delegate.pack_bytes, 641, "Should get packfile");
83 assert_eq!(85 assert_eq!(
84 out.into_inner().1.as_bstr(),86 transport.into_inner().1.as_bstr(),
85 format!(87 format!(
86 "002fgit-upload-pack does/not/matter\0\0version=2\00012command=fetch88 "002fgit-upload-pack does/not/matter\0\0version=2\00012command=fetch
87001aagent={}89001aagent={}
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
61 }61 }
62}62}
63 63
64// Would be nice if the box implementation could auto-forward to all implemented traits.
65#[async_trait(?Send)]
66impl<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
64/// An extension trait to add more methods to everything implementing [`Transport`].80/// An extension trait to add more methods to everything implementing [`Transport`].
65#[async_trait(?Send)]81#[async_trait(?Send)]
66pub trait TransportV2Ext {82pub 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
57 }57 }
58}58}
59 59
60impl<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
60/// An extension trait to add more methods to everything implementing [`Transport`].74/// An extension trait to add more methods to everything implementing [`Transport`].
61pub trait TransportV2Ext {75pub trait TransportV2Ext {
62 /// Invoke a protocol V2 style `command` with given `capabilities` and optional command specific `arguments`.76 /// 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
80 self.deref().is_stateful()80 self.deref().is_stateful()
81 }81 }
82}82}
83
84impl<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}