mirror of
https://github.com/timvisee/lazymc.git
synced 2025-07-31 20:22:02 -07:00
23 lines
753 B
Rust
23 lines
753 B
Rust
use std::error::Error;
|
|
use std::io;
|
|
use tokio::io::AsyncWriteExt;
|
|
use tokio::net::TcpStream;
|
|
|
|
/// Gracefully close given TCP stream.
|
|
///
|
|
/// Intended as helper to make code less messy. This also succeeds if already closed.
|
|
pub async fn close_tcp_stream(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
|
close_tcp_stream_ref(&mut stream).await
|
|
}
|
|
|
|
/// Gracefully close given TCP stream.
|
|
///
|
|
/// Intended as helper to make code less messy. This also succeeds if already closed.
|
|
pub async fn close_tcp_stream_ref(stream: &mut TcpStream) -> Result<(), Box<dyn Error>> {
|
|
match stream.shutdown().await {
|
|
Ok(_) => Ok(()),
|
|
Err(err) if err.kind() == io::ErrorKind::NotConnected => Ok(()),
|
|
Err(err) => Err(err.into()),
|
|
}
|
|
}
|