Repository: Marwes/combine (language: rust)

Issue:
`bytes()` shouldn't require range
I'm writing a byte parser on a `Read` stream, so I do not have `RangeStream` available. Unfortunately, the `bytes()` (and `bytes_cmp()`) combinator requires a Range type of `&[u8]`, which I cannot get from a `ReadStream` (I did find it odd that `BufferedStream` didn't provide that, even if it wasn't a full `RangeStream`). It seems unnecessary for that combinator to require a slice range, especially when something like `string()` combinator does not. In the end, I had to use `tokens2(|a, b| *a == b, expected_bytes)` but that isn't going to give me great errors, plus it clones `expected_bytes` unnecessarily (which is unfortunate since I have a `&'static [u8]` for it) and is definitely not convenient.


Discussion / hints:
Usually the recommendation has been to set `Stream::Item == Stream::Range` if the `Stream` can't implement `RangeStream`. I didn't consider that a proper `Range` could be useful in the error then however so it seems fine to make it a `&'static [u8]` in that case.

The same thing would be useful for `IteratorStream` but unfortunately that can't work since the `Item` may not be `'static`.

Fix patch:
diff --git a/src/parser/byte.rs b/src/parser/byte.rs
index 620f99cf..fddc056f 100644
--- a/src/parser/byte.rs
+++ b/src/parser/byte.rs
@@ -718,6 +718,8 @@ pub mod num {
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::stream::{buffered, state::State, ReadStream};
+
     #[test]
     fn memslice_basic() {
         let haystack = b"abc123";
@@ -733,4 +735,14 @@ mod tests {
         let haystack3 = b"aaabaaaa";
         assert_eq!(memslice(b"aaaa", haystack3), Some(4));
     }
+
+    #[test]
+    fn bytes_read_stream() {
+        assert!(bytes(b"abc")
+            .parse(buffered::Stream::new(
+                State::new(ReadStream::new("abc".as_bytes())),
+                1
+            ))
+            .is_ok());
+    }
 }
diff --git a/src/stream/mod.rs b/src/stream/mod.rs
index 13357693..0a4ceeb4 100644
--- a/src/stream/mod.rs
+++ b/src/stream/mod.rs
@@ -916,9 +916,9 @@ pub struct ReadStream<R> {
 #[cfg(feature = "std")]
 impl<R: Read> StreamOnce for ReadStream<R> {
     type Item = u8;
-    type Range = u8;
+    type Range = &'static [u8];
     type Position = usize;
-    type Error = Errors<u8, u8, usize>;
+    type Error = Errors<Self::Item, Self::Range, usize>;
 
     #[inline]
     fn uncons(&mut self) -> Result<u8, StreamErrorFor<Self>> {


Test patch: