Skip to content

Commit c38b18a

Browse files
committed
Optimize Session::close: Avoid dup code
- Avoid duplicate call to `close_impl` in both `process_impl` and `native_mux_impl` - Extract out code to close `TempDir` out into `Session::close` Signed-off-by: Jiahao XU <[email protected]>
1 parent 685cfc3 commit c38b18a

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

src/native_mux_impl/session.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,13 @@ impl Session {
7676
Ok(())
7777
}
7878

79-
pub(crate) async fn close(mut self) -> Result<(), Error> {
80-
// This also set self.tempdir to None so that Drop::drop would do nothing.
81-
if let Some(tempdir) = self.tempdir.take() {
82-
self.close_impl().await?;
83-
84-
tempdir.close().map_err(Error::Cleanup)?;
85-
} else {
86-
self.close_impl().await?;
87-
}
79+
pub(crate) async fn close(mut self) -> Result<Option<TempDir>, Error> {
80+
// Take self.tempdir so that drop would do nothing
81+
let tempdir = self.tempdir.take();
8882

89-
Ok(())
83+
self.close_impl().await?;
84+
85+
Ok(tempdir)
9086
}
9187

9288
pub(crate) fn detach(mut self) -> (Box<Path>, Option<Box<Path>>) {

src/process_impl/session.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,13 @@ impl Session {
175175
Ok(())
176176
}
177177

178-
pub(crate) async fn close(mut self) -> Result<(), Error> {
178+
pub(crate) async fn close(mut self) -> Result<Option<TempDir>, Error> {
179179
// Take self.tempdir so that drop would do nothing
180-
if let Some(tempdir) = self.tempdir.take() {
181-
self.close_impl().await?;
180+
let tempdir = self.tempdir.take();
182181

183-
tempdir.close().map_err(Error::Cleanup)?;
184-
} else {
185-
self.close_impl().await?;
186-
}
182+
self.close_impl().await?;
187183

188-
Ok(())
184+
Ok(tempdir)
189185
}
190186

191187
pub(crate) fn detach(mut self) -> (Box<Path>, Option<Box<Path>>) {

src/session.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ impl Session {
362362
/// This destructor terminates the ssh multiplex server
363363
/// regardless of how it was created.
364364
pub async fn close(self) -> Result<(), Error> {
365-
delegate!(self.0, imp, { imp.close().await })
365+
let res: Result<Option<TempDir>, Error> = delegate!(self.0, imp, { imp.close().await });
366+
367+
res?.map(TempDir::close)
368+
.transpose()
369+
.map_err(Error::Cleanup)
370+
.map(|_| ())
366371
}
367372

368373
/// Detach the lifetime of underlying ssh multiplex master

0 commit comments

Comments
 (0)