1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Create a [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) object
//! that gets its data incrementally from a function.
//!
//! This lets you read from an a vector of vectors or create
//! a reader that gets blocks from a database or other data source.
//!
//! Example:
//!
//! ```rust
//! let many_strings = ["one", "two", "three"];
//! let mut pos = 0;
//! std::io::copy(
//!     &mut read_with::ReadWith::new(
//!         ||
//!         {
//!             if pos == many_strings.len() { return None; }
//!             let o = many_strings[pos];
//!             pos+=1;
//!             Some(o)
//!         }
//!     ),
//!     &mut std::io::stdout(),
//! ).unwrap();
//! ```

use std::io::Read;

/// An object that implements the `Read` trait
pub struct ReadWith<F, S>
	where F: FnMut() -> Option<S>,
	S: AsRef<[u8]> + Default
{
	f: F,
	current: S,
	offset: usize,
	end: bool
}

impl<F, S> ReadWith<F, S>
	where F: FnMut() -> Option<S>,
	S: AsRef<[u8]> + Default
{
	/// Create an object that will read from the given function.
	///
	/// Keeps on reading from `f` until it returns a None.
	/// The function may return anything that can be turned into
	/// a `&[u8]` which includes `String` and `&str`.
	pub fn new(f: F) -> Self
	{
		ReadWith
		{
			f: f,
			current: Default::default(),
			offset: 0,
			end: false,
		}
	}
}

impl<F,S> Read for ReadWith<F, S>
	where F: FnMut() -> Option<S>,
	S: AsRef<[u8]> + Default
{
	fn read(&mut self, buf: &mut [u8])
		-> std::io::Result<usize>
	{
		let mut wrote = 0;
		while !self.end && wrote < buf.len()
		{
			let count = (buf.len()-wrote).min(self.current.as_ref().len()-self.offset);
			buf[wrote..wrote+count]
				.copy_from_slice( &self.current.as_ref()[self.offset..self.offset+count] );
			wrote += count;
			self.offset += count;
			if self.offset == self.current.as_ref().len()
			{
				self.offset = 0;
				let n = (self.f)();
				if let Some(n) = n
					{ self.current = n; }
				else
					{ self.end = true; }
			}
		}

		Ok(wrote)
	}
}


#[cfg(test)]
mod tests
{
	use ::ReadWith;

	#[test]
	fn references()
	{
		let mut output = vec!();
		let many_strings = ["one", "two", "three"];
		let mut pos = 0;

		::std::io::copy(
			&mut ReadWith::new(
				||
				{
					if pos == many_strings.len() { return None; }
					let o = many_strings[pos];
					pos+=1;
					Some(o)
				}
			),
			&mut output,
		).unwrap();
		assert_eq!("onetwothree", ::std::str::from_utf8(&output).unwrap());
	}

	#[test]
	fn strings()
	{
		let mut output = vec!();
		let many_strings = ["one", "two", "three"];
		let mut pos = 0;
		::std::io::copy(
			&mut ReadWith::new(
				||
				{
					if pos == many_strings.len() { return None; }
					let o = many_strings[pos];
					pos+=1;
					Some(o.to_string() + "\n")
				}
			),
			&mut output,
		).unwrap();
		assert_eq!("one\ntwo\nthree\n", ::std::str::from_utf8(&output).unwrap());
	}
}