Struct arrow_cast::display::ArrayFormatter

source ·
pub struct ArrayFormatter<'a> { /* private fields */ }
Expand description

A string formatter for an Array

This can be used with std::write to write type-erased dyn Array

struct MyContainer {
    values: ArrayRef,
}

impl Display for MyContainer {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let options = FormatOptions::default();
        let formatter = ArrayFormatter::try_new(self.values.as_ref(), &options)
            .map_err(|_| std::fmt::Error)?;

        let mut iter = 0..self.values.len();
        if let Some(idx) = iter.next() {
            write!(f, "{}", formatter.value(idx))?;
        }
        for idx in iter {
            write!(f, ", {}", formatter.value(idx))?;
        }
        Ok(())
    }
}

ValueFormatter::write can also be used to get a semantic error, instead of the opaque std::fmt::Error

fn format_array(
    f: &mut dyn Write,
    array: &dyn Array,
    options: &FormatOptions,
) -> Result<(), ArrowError> {
    let formatter = ArrayFormatter::try_new(array, options)?;
    for i in 0..array.len() {
        formatter.value(i).write(f)?
    }
    Ok(())
}

Implementations§

source§

impl<'a> ArrayFormatter<'a>

source

pub fn try_new( array: &'a dyn Array, options: &FormatOptions<'a>, ) -> Result<Self, ArrowError>

Returns an ArrayFormatter that can be used to format array

This returns an error if an array of the given data type cannot be formatted

source

pub fn value(&self, idx: usize) -> ValueFormatter<'_>

Returns a ValueFormatter that implements Display for the value of the array at idx

Auto Trait Implementations§

§

impl<'a> Freeze for ArrayFormatter<'a>

§

impl<'a> !RefUnwindSafe for ArrayFormatter<'a>

§

impl<'a> !Send for ArrayFormatter<'a>

§

impl<'a> !Sync for ArrayFormatter<'a>

§

impl<'a> Unpin for ArrayFormatter<'a>

§

impl<'a> !UnwindSafe for ArrayFormatter<'a>

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>,

§

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>,

§

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.