read iter ext

master
Avril 5 years ago
parent 60ac9b062c
commit 0edf5e44d6
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -3,3 +3,20 @@
[[package]]
name = "generator-native"
version = "0.1.0"
dependencies = [
"heaparray",
]
[[package]]
name = "heaparray"
version = "0.1.0"
source = "git+https://github.com/notflan/heaparray#6b57c19cf82a71f6bd6e566be7cf36ef1926def7"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f"

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
heaparray = { git = "https://github.com/notflan/heaparray" }

@ -0,0 +1,71 @@
use std::{
io::{
Read,
},
};
use heaparray::{
heap,
};
pub struct BufferedReadIter<F>
where F: Read
{
iter: F,
buffer: Box<[u8]>,
buffer_len: usize,
}
impl<F: Read> Iterator for BufferedReadIter<F>
{
type Item = Box<[u8]>;
fn next(&mut self) -> Option<Self::Item>
{
if self.buffer_len >= self.buffer.len() {
// Buffer is full.
return Some(self.swap_out());
}
if let Ok(read) = self.iter.read(&mut self.buffer[self.buffer_len..])
{
self.buffer_len += read;
if self.buffer_len ==0 {
None
} else {
Some(self.swap_out())
}
} else {
None
}
}
}
impl<F: Read> BufferedReadIter<F>
{
fn swap_out(&mut self) -> Box<[u8]>
{
let len = self.buffer_len;
self.buffer_len=0;
heaparray::box_slice(&mut self.buffer[..len])
}
pub fn new(iter: F, buffer_len: usize) -> Self {
let buffer = heap![u8; buffer_len].into_box();
Self {
iter,
buffer_len: 0,
buffer,
}
}
}
pub trait ReadIterExt: Read + Sized
{
fn into_iter(self, buffer_len: usize) -> BufferedReadIter<Self>
{
BufferedReadIter::new(self, buffer_len)
}
}
impl<T: Read> ReadIterExt for T{}

@ -43,7 +43,6 @@ where I: Iterator<Item=T>
}
}
use super::*;
pub trait GroupExt: Iterator + Sized {
fn group_at(self, at: usize) -> GroupIter<<Self as Iterator>::Item, Self>
{

@ -1,3 +1,12 @@
mod group;
pub use group::*;
mod file;
pub use file::*;
pub mod prelude
{
pub use super::{
ReadIterExt, GroupExt,
};
}

@ -1,5 +1,28 @@
extern crate heaparray;
use std::{
fs::{
self, File, OpenOptions,
},
io,
error::Error,
};
mod iter;
use iter::prelude::*;
fn main() -> Result<(), Box<dyn Error>>{
/*let file = OpenOptions::new()
.read(true)
.open("test.txt")?;
for buf in file.into_iter(2).group_at(2)
{
println!("{:?}", buf);
}*/
fn main() {
println!("Hello, world!");
Ok(())
}

Loading…
Cancel
Save