Struct Encoder

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

Low-level encoder for binary format.

End users are required to manually call all necessary methods in a right order.

use pgcopy::Encoder;

let mut buf: Vec<u8> = vec![];
let mut encoder = Encoder::new(&mut buf);

encoder.write_header().unwrap();

encoder.write_tuple(3).unwrap(); // First tuple with three columns
encoder.write_smallint(1).unwrap(); // First column
encoder.write_bool(false).unwrap(); // Second
encoder.write_str("first").unwrap(); // Third

encoder.write_tuple(3).unwrap(); // Second tuple
encoder.write_smallint(2).unwrap();
encoder.write_bool(true).unwrap();
encoder.write_str("second").unwrap();

encoder.write_trailer().unwrap();

println!("{:?}", encoder.get_ref());

Implementations§

Source§

impl<W> Encoder<W>
where W: Write,

Source

pub fn new(writer: W) -> Encoder<W>

Creates new encoder.

Source

pub fn get_ref(&self) -> &W

Acquires a reference to the underlying writer.

Source

pub fn get_mut(&mut self) -> &mut W

Acquires a mutable reference to the underlying writer.

Note that mutating the output/input state of the stream may corrupt this object, so care must be taken when using this method.

Source

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

Writes binary format header.

Caller is required to invoke this method first before starting to write tuples data.

Source

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

Writes binary format trailer.

Caller is required to invoke this method last immediately after writing tuples data.

Source

pub fn write_tuple(&mut self, fields: i16) -> Result<()>

Starts a new tuple.

Each tuple begins with a signed 16-bit integer count of the number of fields in the tuple. Presently, all tuples in a table will have the same count.

Source

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

Writes NULL as a column value.

Source

pub fn write_smallint(&mut self, value: i16) -> Result<()>

Writes smallint type value.

Source

pub fn write_int(&mut self, value: i32) -> Result<()>

Writes int type value.

Source

pub fn write_bigint(&mut self, value: i64) -> Result<()>

Writes bigint type value.

Source

pub fn write_real(&mut self, value: f32) -> Result<()>

Writes real type value.

Source

pub fn write_double(&mut self, value: f64) -> Result<()>

Writes double precision type value.

Source

pub fn write_str<T: AsRef<str>>(&mut self, value: T) -> Result<()>

Writes character type value.

Any of character varying(n), character(n) or text column type should be handled by this method.

Source

pub fn write_bytea<T: AsRef<[u8]>>(&mut self, value: T) -> Result<()>

Writes bytea type value.

Source

pub fn write_timestamp<T: Timestamp>(&mut self, value: T) -> Result<()>

Writes timestamp type value.

See Timestamp type implementors for available options here.

Source

pub fn write_timestamp_with_time_zone<T: TimestampWithTimeZone>( &mut self, value: T, ) -> Result<()>

Writes timestamp with time zone type value.

See TimestampWithTimeZone type implementors for available options here.

Source

pub fn write_date<T: Date>(&mut self, value: T) -> Result<()>

Writes date type value.

See Date type implementors for available options here.

Source

pub fn write_time<T: Time>(&mut self, value: T) -> Result<()>

Writes time type value.

See Time type implementors for available options here.

Source

pub fn write_bool<T: Into<bool>>(&mut self, value: T) -> Result<()>

Writes bool type value.

Source

pub fn write_macaddr<T: MacAddr>(&mut self, value: T) -> Result<()>

Writes macaddr type value.

See MacAddr type implementors for available options here.

Source

pub fn write_macaddr8<T: MacAddr8>(&mut self, value: T) -> Result<()>

Writes macaddr8 type value.

See MacAddr8 type implementors for available options here.

Source

pub fn write_uuid<T: Uuid>(&mut self, value: T) -> Result<()>

Writes uuid type value.

See Uuid type implementors for available options here.

Trait Implementations§

Source§

impl<W: Clone + Write> Clone for Encoder<W>

Source§

fn clone(&self) -> Encoder<W>

Returns a duplicate 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<W: Debug + Write> Debug for Encoder<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Encoder<W>
where W: Freeze,

§

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

§

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

§

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

§

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

§

impl<W> UnwindSafe for Encoder<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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.