pub struct Output { /* private fields */ }Expand description
Owns an rnp_output_t for the lifetime of this value.
Construct with one of the to_* constructors. For memory destinations,
Output::into_bytes consumes the output and returns the buffered
bytes; for writer destinations, Output::into_writer reclaims the
writer after the handle is closed.
Implementations§
Source§impl Output
impl Output
Sourcepub fn to_memory() -> Result<Self>
pub fn to_memory() -> Result<Self>
Buffer output in memory. Use Output::into_bytes to drain.
max_alloc of 0 means unlimited.
Sourcepub fn to_memory_with_max(max_alloc: usize) -> Result<Self>
pub fn to_memory_with_max(max_alloc: usize) -> Result<Self>
As Output::to_memory, but with an upper bound on the allocation.
Sourcepub fn to_writer(writer: impl Write + 'static) -> Result<Self>
pub fn to_writer(writer: impl Write + 'static) -> Result<Self>
Stream to any std::io::Write.
Chunks are written lazily while the enclosing operation runs, so
nothing is buffered in memory beyond librnp’s own pipeline. On
success the writer is flushed when the output is finished or
dropped; if the operation fails, librnp requests a discard — see
WriterOutcome::discarded. A failed write or flush surfaces as a
failed operation plus the original std::io::Error via
Output::io_error.
let sink = SharedBuf::default();
let input = Input::from_memory(b"stream me").unwrap();
let mut output = Output::to_writer(sink.clone()).unwrap();
output.pipe(&input).unwrap();
drop(output); // closes the stream: flushes on success
assert_eq!(*sink.0.lock().unwrap(), b"stream me");The writer is moved into the Output (the + 'static bound
rules out borrowing a local), so keep a handle to whatever state it
writes into — as above — or reclaim it afterwards with
Output::into_writer. The writer must not be used elsewhere while
the Output exists.
Sourcepub fn to_boxed_writer(writer: Box<dyn Write>) -> Result<Self>
pub fn to_boxed_writer(writer: Box<dyn Write>) -> Result<Self>
As Output::to_writer, for an already-boxed writer.
Sourcepub fn to_file(path: &str, flags: OutputFileFlags) -> Result<Self>
pub fn to_file(path: &str, flags: OutputFileFlags) -> Result<Self>
Write to path with explicit flags (overwrite / random-temp-rename).
Sourcepub fn to_armor(base: &Output, ty: ArmorType) -> Result<Self>
pub fn to_armor(base: &Output, ty: ArmorType) -> Result<Self>
Wrap base with an ASCII-armor encoder. The returned Output owns
the armor handle; base is borrowed for the lifetime of the wrapper
(the C side stores the pointer).
Sourcepub fn write(&mut self, bytes: &[u8]) -> Result<usize>
pub fn write(&mut self, bytes: &[u8]) -> Result<usize>
Write bytes to the output stream. Returns the number of bytes
actually written.
Sourcepub fn finish(&mut self) -> Result<()>
pub fn finish(&mut self) -> Result<()>
Finalize the output. Required for OutputFileFlags::RANDOM (causes
the temp file to be renamed to its final path) and harmless otherwise.
Sourcepub fn set_armor_line_length(&mut self, line_len: usize) -> Result<()>
pub fn set_armor_line_length(&mut self, line_len: usize) -> Result<()>
Override the default armor line length (76). Only meaningful when the
output was created via Output::to_armor.
Sourcepub fn pipe(&mut self, input: &Input) -> Result<()>
pub fn pipe(&mut self, input: &Input) -> Result<()>
Pipe input through to this output until EOF. Consumes neither.
Sourcepub fn into_bytes(self) -> Result<Vec<u8>>
pub fn into_bytes(self) -> Result<Vec<u8>>
Consume a memory-backed output and return its buffered bytes.
Only valid on outputs created via Output::to_memory /
Output::to_memory_with_max.
Sourcepub fn io_error(&self) -> Option<&Error>
pub fn io_error(&self) -> Option<&Error>
The std::io::Error recorded when a writer-backed output’s write
or flush callback last failed, if any. None for non-writer-backed
outputs and when nothing has failed.
Sourcepub fn take_io_error(&mut self) -> Option<Error>
pub fn take_io_error(&mut self) -> Option<Error>
Take the recorded std::io::Error, leaving the slot empty. See
Output::io_error.
Sourcepub fn into_writer(self) -> Option<WriterOutcome>
pub fn into_writer(self) -> Option<WriterOutcome>
Reclaim the writer from a writer-backed Output, destroying the
C handle first. Closing the handle runs librnp’s closer, which
flushes the writer on success or records a discard request on
failure — see WriterOutcome.
Returns None if this output is not writer-backed. If the writer
panicked during an operation, that panic resumes here.