<commit_before>use std::io::{self, Read, Write};

use futures::{Poll, Async};

#[derive(Copy, Clone, Debug)]
pub struct BufRange {
    pub start: usize,
    pub end: usize,
}

#[derive(Copy, PartialEq, Clone, Debug)]
enum BufState {
    Reading,
    Writeing,
    None,
}

pub struct SharedBuf {
    inner: Vec<u8>,
    curr: usize,
    state: BufState,
}

impl SharedBuf {
    pub fn new(size: usize) -> SharedBuf {
        SharedBuf {
            inner: vec![0u8; size],
            curr: 0,
            state: BufState::None,
        }
    }

    pub fn get_ref(&self) -> &[u8] {
        &self.inner[..]
    }

    pub fn get_mut(&mut self) -> &mut [u8] {
        &mut self.inner[..]
    }

    pub fn get_ref_from(&self, from: usize) -> &[u8] {
        assert!(from <= self.inner.len());
        &self.inner[from..]
    }

    pub fn get_mut_from(&mut self, from: usize) -> &mut [u8] {
        assert!(from <= self.inner.len());
        &mut self.inner[from..]
    }

    pub fn get_ref_to(&self, to: usize) -> &[u8] {
        assert!(to <= self.inner.len());
        &self.inner[0..to]
    }

    pub fn get_mut_to(&mut self, to: usize) -> &mut [u8] {
        assert!(to <= self.inner.len());
        &mut self.inner[0..to]
    }

    pub fn get_ref_range(&self, range: BufRange) -> &[u8] {
        let BufRange { start, end } = range;

        assert!(end <= self.inner.len());

        &self.inner[start..end]
    }

    pub fn get_mut_range(&mut self, range: BufRange) -> &mut [u8] {
        let BufRange { start, end } = range;

        assert!(end <= self.inner.len());

        &mut self.inner[start..end]
    }

    pub fn read_exact<R: Read>(
        &mut self,
        reader: &mut R,
        range: BufRange,
    ) -> Poll<BufRange, io::Error> {
        assert!(self.state != BufState::Writeing);

        self.state = BufState::Reading;

        let BufRange { start, end } = range;
        assert!(end <= self.inner.len());

        let need = end - start;
        while self.curr < need {
            let n = try_nb!(reader.read(&mut self.inner[start + self.curr..end]));
            self.curr += n;
            if n == 0 {
                return Err(eof());
            }
        }

        self.curr = 0;
        self.state = BufState::None;
        Ok(Async::Ready(range))
    }

    pub fn write_exact<W: Write>(
        &mut self,
        writer: &mut W,
        range: BufRange,
    ) -> Poll<BufRange, io::Error> {
        assert!(self.state != BufState::Reading);

        self.state = BufState::Writeing;

        let BufRange { start, end } = range;
        assert!(end <= self.inner.len());

        let need = end - start;
        while self.curr < need {
            let n = try_nb!(writer.write(&self.inner[start + self.curr..end]));
            self.curr += n;
            if n == 0 {
                return Err(zero_write());
            }
        }

        self.curr = 0;<commit_msg>add size filed<commit_after>use std::io::{self, Read, Write};

use futures::{Poll, Async};

#[derive(Copy, Clone, Debug)]
pub struct BufRange {
    pub start: usize,
    pub end: usize,
}

#[derive(Copy, PartialEq, Clone, Debug)]
enum BufState {
    Reading,
    Writeing,
    None,
}

pub struct SharedBuf {
    size: usize,
    inner: Vec<u8>,
    curr: usize,
    state: BufState,
}

impl SharedBuf {
    pub fn new(size: usize) -> SharedBuf {
        SharedBuf {
            size: size,
            inner: vec![0u8; size],
            curr: 0,
            state: BufState::None,
        }
    }

    pub fn get_ref(&self) -> &[u8] {
        &self.inner[..]
    }

    pub fn get_mut(&mut self) -> &mut [u8] {
        &mut self.inner[..]
    }

    pub fn get_ref_from(&self, from: usize) -> &[u8] {
        assert!(from <= self.size);
        &self.inner[from..]
    }

    pub fn get_mut_from(&mut self, from: usize) -> &mut [u8] {
        assert!(from <= self.size);
        &mut self.inner[from..]
    }

    pub fn get_ref_to(&self, to: usize) -> &[u8] {
        assert!(to <= self.size);
        &self.inner[0..to]
    }

    pub fn get_mut_to(&mut self, to: usize) -> &mut [u8] {
        assert!(to <= self.size);
        &mut self.inner[0..to]
    }

    pub fn get_ref_range(&self, range: BufRange) -> &[u8] {
        let BufRange { start, end } = range;

        assert!(end <= self.size);

        &self.inner[start..end]
    }

    pub fn get_mut_range(&mut self, range: BufRange) -> &mut [u8] {
        let BufRange { start, end } = range;

        assert!(end <= self.size);

        &mut self.inner[start..end]
    }

    pub fn read_exact<R: Read>(
        &mut self,
        reader: &mut R,
        range: BufRange,
    ) -> Poll<BufRange, io::Error> {
        assert!(self.state != BufState::Writeing);

        self.state = BufState::Reading;

        let BufRange { start, end } = range;
        assert!(end <= self.size);

        let need = end - start;
        while self.curr < need {
            let n = try_nb!(reader.read(&mut self.inner[start + self.curr..end]));
            self.curr += n;
            if n == 0 {
                return Err(eof());
            }
        }

        self.curr = 0;
        self.state = BufState::None;
        Ok(Async::Ready(range))
    }

    pub fn write_exact<W: Write>(
        &mut self,
        writer: &mut W,
        range: BufRange,
    ) -> Poll<BufRange, io::Error> {
        assert!(self.state != BufState::Reading);

        self.state = BufState::Writeing;

        let BufRange { start, end } = range;
        assert!(end <= self.size);

        let need = end - start;
        while self.curr < need {
            let n = try_nb!(writer.write(&self.inner[start + self.curr..end]));
            self.curr += n;
            if n == 0 {
                return Err(zero_write());
            }
        }

        self.curr = 0;
{'fraction_non_alphanumeric': 0.15934065934065933, 'fraction_numerical': 0.006181318681318681, 'mean_word_length': 1.764594209776934, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4499443', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1940, 'n_words': 465}