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>
impl<W: Write> Writer<W>
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Creates a writer that uses \n as the default separator.
デフォルト区切り \n でライターを作成します。
Sourcepub fn with_separator(self, separator: RecordSeparator) -> Self
pub fn with_separator(self, separator: RecordSeparator) -> Self
Changes the separator used after each record. レコードごとの区切りを変更します。
Sourcepub fn write_record<T: FixedRecord>(&mut self, record: &T) -> Result<()>
pub fn write_record<T: FixedRecord>(&mut self, record: &T) -> Result<()>
Writes one record. レコードを1件書き込みます。
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more