Skip to content

Commit

Permalink
Merge pull request #2
Browse files Browse the repository at this point in the history
impl Read and Write
  • Loading branch information
cubicle-jockey committed Jul 4, 2023
2 parents 0693796 + 7072381 commit 1014da8
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/ascii_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::vec_deque::{Iter, IterMut};
use std::collections::VecDeque;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::io::{Read, Write};
use std::iter::Map;
use std::ops::{Add, AddAssign, Index, IndexMut};

Expand Down Expand Up @@ -446,6 +447,44 @@ impl AsciiString {
self.bytes.make_contiguous();
self.bytes.as_mut_slices().0
}

/// Fills buf with the contents of the `AsciiString`, returning the number of bytes read.
/// * this consumes the bytes read from the `AsciiString`.
/// # Example
///```
/// # use cj_ascii::prelude::*;
/// let mut astring = AsciiString::new();
/// astring += "ABC";
/// let mut buf = [0u8; 3];
/// let result = astring.read(&mut buf);
///
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap(), 3);
/// assert_eq!(buf, [65, 66, 67]);
/// // astring is now empty
/// assert_eq!(astring.as_bytes(), []);
/// assert_eq!(astring.len(), 0);
/// ```
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.bytes.make_contiguous();
self.bytes.read(buf)
}
/// Writes buf into the `AsciiString`, returning the number of bytes written.
/// # Example
/// ```
/// # use cj_ascii::prelude::*;
/// let mut string = AsciiString::new();
/// let buf = [65, 66, 67];
/// let result = string.write(&buf);
///
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap(), 3);
/// assert_eq!(string.as_bytes(), [65, 66, 67]);
/// assert_eq!(string.len(), 3);
/// ```
pub fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
self.bytes.write(buf)
}
/// Sorts the `AsciiString` in place.
/// # Example
/// ```
Expand Down Expand Up @@ -1287,6 +1326,76 @@ mod test {
assert!(!string.contains('D'));
}

#[test]
fn test_read() {
use crate::ascii_string::AsciiString;
let mut string = AsciiString::new();
string += "ABC";
let mut buf = [0u8; 3];
let result = string.read(&mut buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 3);
assert_eq!(buf, [65, 66, 67]);
assert_eq!(string.as_bytes(), []);
assert_eq!(string.len(), 0);
}

#[test]
fn test_read_undersized() {
use crate::ascii_string::AsciiString;
let mut string = AsciiString::new();
string += "ABC";
let mut buf = [0u8; 2];
let result = string.read(&mut buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 2);
assert_eq!(buf, [65, 66]);
assert_eq!(string.as_bytes(), [67]);
assert_eq!(string.len(), 1);

buf.fill(0);
let result = string.read(&mut buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 1);
assert_eq!(buf, [67, 0]);
assert_eq!(string.as_bytes(), []);
assert_eq!(string.len(), 0);

buf.fill(0);
let result = string.read(&mut buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 0);
assert_eq!(buf, [0, 0]);
assert_eq!(string.as_bytes(), []);
assert_eq!(string.len(), 0);
}

#[test]
fn test_read_oversized() {
use crate::ascii_string::AsciiString;
let mut string = AsciiString::new();
string += "ABC";
let mut buf = [0u8; 4];
let result = string.read(&mut buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 3);
assert_eq!(buf, [65, 66, 67, 0]);
assert_eq!(string.as_bytes(), []);
assert_eq!(string.len(), 0);
}

#[test]
fn test_write() {
use crate::ascii_string::AsciiString;
let mut string = AsciiString::new();
let buf = [65, 66, 67];
let result = string.write(&buf);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 3);
assert_eq!(string.as_bytes(), [65, 66, 67]);
assert_eq!(string.len(), 3);
}

#[test]
fn test_iter_mut() {
use crate::ascii_string::AsciiString;
Expand Down

0 comments on commit 1014da8

Please sign in to comment.