Skip to main content

Writer

Struct Writer 

Source
pub struct Writer<W: Write> { /* private fields */ }
Expand description

Writer for fixed-width records. 固定長レコードをストリームへ書き出すライターです。

During writing, NUL bytes (0x00) are replaced with spaces (0x20) and a separator is appended. 書き込み時は NUL (0x00) をスペース (0x20) に置換し、レコード末尾に区切りを付けます。

The inner writer is flushed automatically when Writer is dropped, but explicit Writer::flush is recommended when callers need to handle I/O errors before the writer goes out of scope. Writer が drop されるときに内部 writer は自動的に flush されます。ただし、I/O エラーを明示的に扱いたい場合は Writer::flush を呼び出してください。

§Examples

use fixed_record::prelude::*;

#[fixed_record]
pub struct Payment {
    id: Fixed<4>,
    amount: Fixed<6>,
}

let first = Payment::builder()
    .with_id("A001")
    .with_amount_int(1200)
    .build();
let second = Payment::builder()
    .with_id("A002")
    .with_amount_int(3400)
    .build();

let mut output = Vec::new();
let mut writer = Writer::new(&mut output)
    .with_separator(RecordSeparator::Comma);

writer.write_all([&first, &second]).unwrap();
writer.flush().unwrap();
drop(writer);

let mut expected = Vec::new();
expected.extend_from_slice(&first.to_bytes());
expected.push(b',');
expected.extend_from_slice(&second.to_bytes());
expected.push(b',');

assert_eq!(output, expected);

Implementations§

Source§

impl<W: Write> Writer<W>

Source

pub fn new(writer: W) -> Self

Creates a writer that uses \n as the default separator. デフォルト区切り \n でライターを作成します。

Source

pub fn with_separator(self, separator: RecordSeparator) -> Self

Changes the separator used after each record. レコードごとの区切りを変更します。

Source

pub fn write_record<T: FixedRecord>(&mut self, record: &T) -> Result<()>

Writes one record. レコードを1件書き込みます。

Source

pub fn write_all<'a, T, I>(&mut self, records: I) -> Result<()>
where T: FixedRecord + 'a, I: IntoIterator<Item = &'a T>,

Writes all borrowed records in iterator order. 借用した全レコードをイテレータの順序で書き込みます。

Each record uses the configured separator. Writing stops at the first I/O error. 各レコードには設定済みの区切りを使います。最初の I/O エラーで書き込みを停止します。

Source

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

Flushes the inner writer. 内部ライターを flush します。

Trait Implementations§

Source§

impl<W: Write> Drop for Writer<W>

Source§

fn drop(&mut self)

Flushes the inner writer when this writer goes out of scope. スコープを抜けるときに内部 writer を flush します。

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<W> Freeze for Writer<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Writer<W>
where W: RefUnwindSafe,

§

impl<W> Send for Writer<W>
where W: Send,

§

impl<W> Sync for Writer<W>
where W: Sync,

§

impl<W> Unpin for Writer<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for Writer<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for Writer<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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.