add close/{1,2}, better EOF action handling in read_term

This commit is contained in:
Mark Thom
2020-05-05 00:38:21 -06:00
parent dd247cd541
commit ab62603c5a
4 changed files with 90 additions and 16 deletions

View File

@@ -12,7 +12,7 @@ use std::fmt;
use std::fs::File;
use std::io::{stdout, Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
use std::hash::{Hash, Hasher};
use std::net::TcpStream;
use std::net::{Shutdown, TcpStream};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -42,6 +42,18 @@ pub enum StreamInstance {
TcpStream(TcpStream),
}
impl Drop for StreamInstance {
fn drop(&mut self) {
match self {
StreamInstance::TcpStream(ref mut tcp_stream) => {
tcp_stream.shutdown(Shutdown::Both).unwrap();
}
_ => {
}
}
}
}
impl fmt::Debug for StreamInstance {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
@@ -140,12 +152,21 @@ impl Default for StreamOptions {
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Hash)]
pub struct Stream {
pub options: StreamOptions,
stream_inst: WrappedStreamInstance,
}
impl PartialEq for Stream {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.stream_inst == other.stream_inst
}
}
impl Eq for Stream {}
impl From<TcpStream> for Stream {
fn from(tcp_stream: TcpStream) -> Self {
tcp_stream.set_read_timeout(None).unwrap();
@@ -292,6 +313,12 @@ impl Stream {
}
}
#[inline]
pub(crate)
fn close(&mut self) {
*self.stream_inst.0.borrow_mut() = StreamInstance::Null;
}
#[inline]
pub(crate)
fn is_input_stream(&self) -> bool {