Incremental UTF8 decoder which convert [AsyncRead
][] into [Stream
][] of futures-rs.
```rust use anyhow::Result; use asyncstd::io::Cursor; use asyncstd::future::timeout; use asyncstd::prelude::*; use std::time::Duration; use asyncutf8_decoder::Utf8Decoder;
async fn test() -> Result<()> { let dur = Duration::from_millis(10); let cur = Cursor::new(Vec::new()); let mut decoder = Utf8Decoder::new(cur);
async fn append(cursor: &mut Cursor<Vec<u8>>, data: &[u8]) -> Result<()> {
let p = cursor.position();
cursor.set_position(cursor.get_ref().len() as u64);
cursor.write(data).await?;
cursor.set_position(p);
Ok(())
}
// Decode full
append(decoder.get_mut(), &vec![240, 159, 146, 150]).await?;
let decoded = timeout(dur, decoder.next()).await?.unwrap()?;
assert_eq!("💖", decoded);
// Decode half
append(decoder.get_mut(), &vec![240, 159]).await?;
assert!(timeout(dur, decoder.next()).await.is_err());
append(decoder.get_mut(), &vec![146, 150]).await?;
let decoded = timeout(dur, decoder.next()).await?.unwrap()?;
assert_eq!("💖", decoded);
// Decode char
append(decoder.get_mut(), &vec![240]).await?;
assert!(timeout(dur, decoder.next()).await.is_err());
append(decoder.get_mut(), &vec![159]).await?;
assert!(timeout(dur, decoder.next()).await.is_err());
append(decoder.get_mut(), &vec![146]).await?;
assert!(timeout(dur, decoder.next()).await.is_err());
append(decoder.get_mut(), &vec![150]).await?;
let decoded = timeout(dur, decoder.next()).await?.unwrap()?;
assert_eq!("💖", decoded);
// Decode lot
append(
decoder.get_mut(),
&vec![
240, 159, 146, 150, 240, 159, 146, 150, 240, 159, 146, 150, 240, 159, 146, 150,
240, 159, 146, 150,
],
)
.await?;
let decoded = timeout(dur, decoder.next()).await?.unwrap()?;
assert_eq!("💖💖💖💖💖", decoded);
Ok(())
} ```
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.