indent_write::io

Struct IndentWriter

Source
pub struct IndentWriter<'i, W> { /* private fields */ }
Expand description

Adapter for writers to indent each line

An IndentWriter adapts an io::Write object to insert an indent before each non-empty line. Specifically, this means it will insert an indent between each newline when followed by a non-newline.

These writers can be nested to provide increasing levels of indentation.

§Example

use indent_write::io::IndentWriter;

let output = Vec::new();

let mut indented = IndentWriter::new("\t", output);

// Lines will be indented
write!(indented, "Line 1\nLine 2\n");

// Empty lines will not be indented
write!(indented, "\n\nLine 3\n\n");

assert_eq!(indented.get_ref(), b"\tLine 1\n\tLine 2\n\n\n\tLine 3\n\n");

Implementations§

Source§

impl<'i, W: Write> IndentWriter<'i, W>

Source

pub fn new(indent: &'i str, writer: W) -> Self

Create a new IndentWriter.

Source

pub fn new_skip_initial(indent: &'i str, writer: W) -> Self

Create a new IndentWriter which will not add an indent to the first written line.

§Example
use indent_write::io::IndentWriter;

let mut buffer = Vec::new();
let mut writer = IndentWriter::new_skip_initial("    ", &mut buffer);

writeln!(writer, "Line 1").unwrap();
writeln!(writer, "Line 2").unwrap();
writeln!(writer, "Line 3").unwrap();

assert_eq!(buffer, b"Line 1\n    Line 2\n    Line 3\n")
Source

pub fn into_inner(self) -> W

Extract the writer from the IndentWriter, discarding any in-progress indent state.

Source

pub fn get_ref(&self) -> &W

Get a reference to the wrapped writer

Source

pub fn indent(&self) -> &'i str

Get the string being used as an indent for each line

Trait Implementations§

Source§

impl<'i, W: Clone> Clone for IndentWriter<'i, W>

Source§

fn clone(&self) -> IndentWriter<'i, W>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'i, W: Debug> Debug for IndentWriter<'i, W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'i, W: Write> Write for IndentWriter<'i, W>

Source§

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

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'i, W> Freeze for IndentWriter<'i, W>
where W: Freeze,

§

impl<'i, W> RefUnwindSafe for IndentWriter<'i, W>
where W: RefUnwindSafe,

§

impl<'i, W> Send for IndentWriter<'i, W>
where W: Send,

§

impl<'i, W> Sync for IndentWriter<'i, W>
where W: Sync,

§

impl<'i, W> Unpin for IndentWriter<'i, W>
where W: Unpin,

§

impl<'i, W> UnwindSafe for IndentWriter<'i, W>
where W: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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.