rust-postgres/postgres-shared/src/lib.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2016-12-21 00:07:45 +00:00
#![allow(unknown_lints)] // for clippy
2016-12-20 23:33:16 +00:00
extern crate hex;
2016-12-21 00:07:45 +00:00
extern crate fallible_iterator;
extern crate phf;
extern crate postgres_protocol;
2016-12-20 23:33:16 +00:00
2016-12-21 03:50:44 +00:00
use fallible_iterator::{FallibleIterator, FromFallibleIterator};
use std::ops::Range;
2016-12-21 00:07:45 +00:00
pub mod error;
2016-12-20 23:33:16 +00:00
pub mod params;
2016-12-21 16:14:24 +00:00
pub mod types;
2016-12-21 03:50:44 +00:00
pub struct RowData {
buf: Vec<u8>,
indices: Vec<Option<Range<usize>>>,
}
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
where I: FallibleIterator<Item = Option<&'a [u8]>>
{
let mut row = RowData {
buf: vec![],
indices: Vec::with_capacity(it.size_hint().0),
};
while let Some(cell) = try!(it.next()) {
let index = match cell {
Some(cell) => {
let base = row.buf.len();
row.buf.extend_from_slice(cell);
Some(base..row.buf.len())
}
None => None,
};
row.indices.push(index);
}
Ok(row)
}
}
impl RowData {
pub fn len(&self) -> usize {
self.indices.len()
}
pub fn get(&self, index: usize) -> Option<&[u8]> {
match &self.indices[index] {
&Some(ref range) => Some(&self.buf[range.clone()]),
&None => None,
}
}
}