Skip to main content

Output

Struct Output 

Source
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

Source

pub fn to_memory() -> Result<Self>

Buffer output in memory. Use Output::into_bytes to drain.

max_alloc of 0 means unlimited.

Source

pub fn to_memory_with_max(max_alloc: usize) -> Result<Self>

As Output::to_memory, but with an upper bound on the allocation.

Source

pub fn to_null() -> Result<Self>

Discard all output.

Source

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.

Source

pub fn to_boxed_writer(writer: Box<dyn Write>) -> Result<Self>

As Output::to_writer, for an already-boxed writer.

Source

pub fn to_path(path: &str) -> Result<Self>

Write to path, overwriting if it already exists.

Source

pub fn to_file(path: &str, flags: OutputFileFlags) -> Result<Self>

Write to path with explicit flags (overwrite / random-temp-rename).

Source

pub fn to_stdout() -> Result<Self>

Write to process stdout.

Source

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).

Source

pub fn write(&mut self, bytes: &[u8]) -> Result<usize>

Write bytes to the output stream. Returns the number of bytes actually written.

Source

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.

Source

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.

Source

pub fn pipe(&mut self, input: &Input) -> Result<()>

Pipe input through to this output until EOF. Consumes neither.

Source

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.

Source

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.

Source

pub fn take_io_error(&mut self) -> Option<Error>

Take the recorded std::io::Error, leaving the slot empty. See Output::io_error.

Source

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.

Trait Implementations§

Source§

impl Drop for Output

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Output

§

impl !Send for Output

§

impl !Sync for Output

§

impl !UnwindSafe for Output

§

impl Freeze for Output

§

impl Unpin for Output

§

impl UnsafeUnpin for Output

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.