Skip to content

Commit

Permalink
Make Stream::Null behave like /dev/null
Browse files Browse the repository at this point in the history
  • Loading branch information
adri326 committed Feb 2, 2025
1 parent fdc35f8 commit c31e369
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ impl Machine {
.insert(atom!("user_error"), self.user_error);

self.indices.streams.insert(self.user_error);

self.indices
.stream_aliases
.insert(atom!("null_stream"), Stream::Null(StreamOptions::default()));
}

#[inline(always)]
Expand Down
74 changes: 41 additions & 33 deletions src/machine/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,13 @@ impl Read for Stream {
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
)),
Stream::OutputFile(_)
| Stream::StandardError(_)
| Stream::StandardOutput(_)
| Stream::Null(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
)),
Stream::Null(_) => Ok(buf.len()),
Stream::OutputFile(_) | Stream::StandardError(_) | Stream::StandardOutput(_) => {
Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
))
}
}
}
}
Expand All @@ -864,13 +864,10 @@ impl Write for Stream {
ErrorKind::PermissionDenied,
StreamError::WriteToInputStream,
)),
Stream::StaticString(_)
| Stream::Readline(_)
| Stream::InputFile(..)
| Stream::Null(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::WriteToInputStream,
)),
Stream::Null(_) => Ok(buf.len()),
Stream::StaticString(_) | Stream::Readline(_) | Stream::InputFile(..) => Err(
std::io::Error::new(ErrorKind::PermissionDenied, StreamError::WriteToInputStream),
),
}
}

Expand All @@ -890,13 +887,10 @@ impl Write for Stream {
ErrorKind::PermissionDenied,
StreamError::FlushToInputStream,
)),
Stream::StaticString(_)
| Stream::Readline(_)
| Stream::InputFile(_)
| Stream::Null(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::FlushToInputStream,
)),
Stream::Null(_) => Ok(()),
Stream::StaticString(_) | Stream::Readline(_) | Stream::InputFile(_) => Err(
std::io::Error::new(ErrorKind::PermissionDenied, StreamError::FlushToInputStream),
),
}
}
}
Expand Down Expand Up @@ -1144,6 +1138,7 @@ impl Stream {
}
}
}
Stream::Null(_) => AtEndOfStream::At,
#[cfg(feature = "http")]
Stream::HttpRead(stream_layout) => {
if stream_layout
Expand Down Expand Up @@ -1356,7 +1351,8 @@ impl Stream {
| Stream::Byte(_)
| Stream::Readline(_)
| Stream::StaticString(_)
| Stream::InputFile(..) => true,
| Stream::InputFile(..)
| Stream::Null(_) => true,
_ => false,
}
}
Expand All @@ -1372,7 +1368,8 @@ impl Stream {
| Stream::StandardOutput(_)
| Stream::NamedTcp(..)
| Stream::Byte(_)
| Stream::OutputFile(..) => true,
| Stream::OutputFile(..)
| Stream::Null(_) => true,
_ => false,
}
}
Expand Down Expand Up @@ -1607,7 +1604,7 @@ impl MachineState {
debug_assert_eq!(arity, 0);

return match stream_aliases.get(&name) {
Some(stream) if !stream.is_null_stream() => Ok(*stream),
Some(stream) => Ok(*stream),
_ => {
let stub = functor_stub(caller, arity);
let addr = atom_as_cell!(name);
Expand All @@ -1625,7 +1622,7 @@ impl MachineState {
debug_assert_eq!(arity, 0);

return match stream_aliases.get(&name) {
Some(stream) if !stream.is_null_stream() => Ok(*stream),
Some(stream) => Ok(*stream),
_ => {
let stub = functor_stub(caller, arity);
let addr = atom_as_cell!(name);
Expand All @@ -1639,11 +1636,10 @@ impl MachineState {
(HeapCellValueTag::Cons, ptr) => {
match_untyped_arena_ptr!(ptr,
(ArenaHeaderTag::Stream, stream) => {
return if stream.is_null_stream() {
Err(self.open_permission_error(HeapCellValue::from(stream), caller, arity))
} else {
Ok(stream)
};
if stream.is_null_stream() {
unreachable!("Null streams have no Cons representation");
}
Ok(stream)
}
(ArenaHeaderTag::Dropped, _value) => {
let stub = functor_stub(caller, arity);
Expand Down Expand Up @@ -1936,7 +1932,11 @@ mod test {
let results = machine.run_query("get_code(C).").collect::<Vec<_>>();

assert_eq!(results.len(), 1);
assert!(results[0].is_err());
assert!(
results[0].is_ok(),
"Expected read to succeed, got {:?}",
results[0]
);
}

#[test]
Expand Down Expand Up @@ -1966,7 +1966,11 @@ mod test {
let results = machine.run_query("write(hello).").collect::<Vec<_>>();

assert_eq!(results.len(), 1);
assert!(results[0].is_err());
assert!(
results[0].is_ok(),
"Expected write to succeed, got {:?}",
results[0]
);
}

/// A variant of the [`write_null_stream`] that tries to write to a (null) input stream.
Expand All @@ -1982,6 +1986,10 @@ mod test {
.collect::<Vec<_>>();

assert_eq!(results.len(), 1);
assert!(results[0].is_err());
assert!(
results[0].is_ok(),
"Expected write to succeed, got {:?}",
results[0]
);
}
}

0 comments on commit c31e369

Please sign in to comment.