display_buffered 0.1.1

A small library that provides convinience functions to write all the elements into a writer with buffering
Documentation
//! Provides convinience functions to write all the elements into a writer with buffering

use std::{
    fmt::Display,
    io::{self, BufWriter, Write},
};

/// Writes all the items into the writer with buffering sepparating them with the new line
///
/// # Arguments
///
/// * values - slice of values to print
/// * buf - buffer to write into
///
/// # Errors
///
/// Errors if the writing or flushing the buffer errors
///
/// # Example
///
/// ```
/// use display_buffered::display_buffered;
///
/// use std::io::stdout;
///
/// display_buffered([10, 20, 30], stdout()).unwrap()
/// ```
pub fn display_buffered<I, T, W>(values: I, writer: W) -> Result<(), io::Error>
where
    T: Display,
    I: IntoIterator<Item = T>,
    W: Write,
{
    let values = values.into_iter().map(|item| format!("{item}\n"));

    write_buffered_strings(values, writer)
}

/// Writes all the strings from values into the writer with buffering without any separators
///
/// # Arguments
///
/// * values - slice of strings to print
/// * buf - buffer to write into
///
/// # Errors
///
/// Errors if the writing or flushing the buffer errors
///
/// # Example
///
/// ```
/// use display_buffered::write_buffered_strings;
///
/// use std::io::stdout;
///
/// write_buffered_strings(["10", "20", "30"], stdout()).unwrap() // Prints 102030
/// ```
pub fn write_buffered_strings<T, I, W>(values: I, writer: W) -> Result<(), io::Error>
where
    T: AsRef<str>,
    I: IntoIterator<Item = T>,
    W: Write,
{
    let mut writer = BufWriter::new(writer);

    for string in values {
        writer.write_all(string.as_ref().as_bytes())?
    }

    writer.flush()
}