Skip to content

Commit

Permalink
docs: add usage examples to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetardev committed Mar 24, 2022
1 parent 602f566 commit a109932
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: Run tests
run: cargo test --verbose
- name: Publish
run: cargo release
run: cargo publish
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "md5-rs"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ pub use consts::{DIGEST_LEN, INPUT_BUFFER_LEN};

#[derive(Debug)]
pub struct Context {
/// The total size of the recieved input
size: u64,
/// The input buffer
///
/// Note: only access directly if you're writing to it, (e.g. if you want to write to it via Wasm memory)
pub input: [u8; INPUT_BUFFER_LEN],
/// The buffer for the digest
digest: [u8; DIGEST_LEN],
/// The working buffer
buffer: [u32; 4],
}

Expand All @@ -36,6 +42,12 @@ impl Context {
}

/// Process the bytes in `buf`
///
/// Usage:
/// ```rs
/// let mut ctx = Context::new();
/// ctx.read(b"hello world");
/// ```
pub fn read(&mut self, buf: &[u8]) {
let mut offset = (self.size % BLOCK_SIZE as u64) as usize;
self.size += buf.len() as u64;
Expand Down Expand Up @@ -91,6 +103,15 @@ impl Context {
}

/// Closes the reader and returns the digest
///
/// Usage:
/// ```rs
/// let mut ctx = Context::new();
/// ctx.read(b"hello world");
/// let digest = ctx.finish();
/// // prints the actual hash bytes, you need to do the hex string yourself
/// println!("{:?}", digest);
/// ```
pub fn finish(mut self) -> [u8; DIGEST_LEN] {
// Insert the padding
let offset = (self.size % (BLOCK_SIZE as u64)) as usize;
Expand Down

0 comments on commit a109932

Please sign in to comment.