indent_write::fmt

Struct IndentWriter

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

Adapter for writers to indent each line

An IndentWriter adapts a fmt::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::fmt::IndentWriter;

let output = String::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(), "\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::fmt::IndentWriter;

let mut buffer = String::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, "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_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
Source§

fn write_char(&mut self, c: char) -> Result

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

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

Glue for usage of the write! macro with implementors of this trait. 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.