### Rust Benchmark Setup - Benchmarking Hex Encoding/Decoding Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md This Rust code snippet demonstrates the setup for benchmarking hex encoding and decoding functions. It attributes the origin of the benchmark methodology to the 'rust-hex' crate and compares the performance of 'array-bytes' functions against several other popular hex crates. ```rust //! The origin benchmark comes from [rust-hex](https://github.com/KokaKiwi/rust-hex/blob/main/benches/hex.rs). //! Thanks for their previous works. ``` -------------------------------- ### Rust Fuzzing Setup - Dehexify Trait Implementations Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md This Rust code snippet shows the setup for fuzz testing the 'Dehexify' trait implementations for various unsigned integer types and other hex functions within the 'array-bytes' crate. It uses 'libfuzzer_sys' to define fuzz targets, ensuring robustness against malformed input. ```rust use array_bytes::{Dehexify, Hexify}; #[cfg(feature = "libfuzzer_sys")])] #[macro_use] extern crate libfuzzer_sys; // Fuzz target for Dehexify trait implementations for various unsigned integer types fuzz_target!(|data: &[u8]| { let _ = usize::from_hex(data); let _ = u8::from_hex(data); let _ = u16::from_hex(data); let _ = u32::from_hex(data); let _ = u64::from_hex(data); let _ = u128::from_hex(data); // Fuzz target for hexify function let _ = data.hexify(); // Fuzz target for dehexify_slice_mut function let mut buffer = vec![0u8; data.len()]; let _ = array_bytes::dehexify_slice_mut(data, &mut buffer); }); ``` -------------------------------- ### Rust Inline Function Hint for Performance Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md Rust attribute used to suggest to the compiler that a function should always be inlined. This is a performance optimization technique applied to frequently called or critical path functions to reduce function call overhead. Examples like `dehexify_array` and `strip_0x` utilize this. ```rust #[inline(always)] fn dehexify_array(...) { ... } ``` ```rust #[inline(always)] fn strip_0x(...) { ... } ``` -------------------------------- ### Rust Crate Quality and Best Practices Configuration Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md Configuration snippet showcasing Rust's linting and quality enforcement using `clippy` and `rustc`. It defines a policy to deny all clippy lints and requires documentation and usage of crate dependencies, with exceptions for specific lints. This is typically found at the crate root (`lib.rs` or `main.rs`). ```rust #![deny(clippy::all, missing_docs, unused_crate_dependencies)] // Allow specific clippy lints where necessary // Example: // #![allow(clippy::items_after_test_module)] // #![allow(clippy::tabs_in_doc_comments)] ``` -------------------------------- ### Run array-bytes Benchmarks in Rust Source: https://github.com/hack-ink/array-bytes/blob/main/README.md Provides instructions to clone the array-bytes repository, navigate to its directory, and execute the benchmark tests using Cargo. This allows users to verify the performance of the hexify and dehexify operations against other libraries. ```sh git clone https://github.com/hack-ink/array-bytes cd array-bytes cargo bench ``` -------------------------------- ### Unsafe Byte Array Length Setting in Rust Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md This snippet demonstrates the unsafe usage of `set_len` on a byte vector in Rust, often found in performance-critical code where manual length management is required. It's crucial to ensure capacity and bounds are correctly handled before this operation to prevent undefined behavior. This pattern is observed in `src/hex/dehexify.rs`. ```rust unsafe { bytes.set_len(cap); } ``` -------------------------------- ### Hexify and Dehexify Byte Slices and Numeric Types in Rust Source: https://github.com/hack-ink/array-bytes/blob/main/README.md Demonstrates the usage of Hexify and Dehexify traits from the array-bytes crate. It covers converting various unsigned integer types (u8, u16, u32, u64, u128, usize), byte arrays ([u8; N]), byte slices (&[u8]), Vec, and SmallVec to and from hex string representations. It also shows error handling for invalid hex input during dehexification. ```rust use array_bytes::{Dehexify, Error, Hexify}; use smallvec::SmallVec; // Hexify. // Unsigned. assert_eq!(52_u8.hexify(), "34"); assert_eq!(520_u16.hexify_upper(), "208"); assert_eq!(5_201_314_u32.hexify_prefixed(), "0x4f5da2"); assert_eq!(5_201_314_u64.hexify_prefixed_upper(), "0x4F5DA2"); assert_eq!(5_201_314_u128.hexify(), "4f5da2"); assert_eq!(5_201_314_usize.hexify_upper(), "4F5DA2"); // `[u8; N]`. assert_eq!(*b"Love Jane Forever".hexify(), String::from("4c6f7665204a616e6520466f7265766572")); // `&[u8; N]`. assert_eq!( b"Love Jane Forever".hexify_upper(), String::from("4C6F7665204A616E6520466F7265766572") ); // `&[u8]`. assert_eq!( b"Love Jane Forever".as_slice().hexify_prefixed(), String::from("0x4c6f7665204a616e6520466f7265766572") ); // `Vec`. assert_eq!( b"Love Jane Forever".to_vec().hexify_prefixed_upper(), String::from("0x4C6F7665204A616E6520466F7265766572") ); // `&Vec`. assert_eq!( (&b"Love Jane Forever".to_vec()).hexify(), String::from("4c6f7665204a616e6520466f7265766572") ); // Dehexify. // Unsigned. assert_eq!(u8::dehexify("34"), Ok(52)); assert_eq!(u16::dehexify("208"), Ok(520)); assert_eq!(u32::dehexify("0x4f5da2"), Ok(5_201_314)); assert_eq!(u64::dehexify("0x4F5DA2"), Ok(5_201_314)); assert_eq!(u128::dehexify("4f5da2"), Ok(5_201_314)); assert_eq!(usize::dehexify("4F5DA2"), Ok(5_201_314)); // Array. assert_eq!( <[u8; 17]>::dehexify("0x4c6f7665204a616e6520466f7265766572"), Ok(*b"Love Jane Forever") ); // `SmallVec`. assert_eq!( SmallVec::dehexify("0x4c6f7665204a616e6520466f7265766572").unwrap().into_vec(), b"Love Jane Forever".to_vec() ); assert_eq!(SmallVec::dehexify("我爱你"), Err(Error::InvalidLength)); assert_eq!(SmallVec::dehexify("0x我爱你"), Err(Error::InvalidLength)); // `Vec`. assert_eq!( >::dehexify("0x4c6f7665204a616e6520466f7265766572"), Ok(b"Love Jane Forever".to_vec()) ); assert_eq!( >::dehexify("我爱你 "), Err(Error::InvalidCharacter { character: 'æ', index: 0 }) ); assert_eq!( >::dehexify(" 我爱你"), Err(Error::InvalidCharacter { character: ' ', index: 0 }) ); ``` -------------------------------- ### Unsafe UTF-8 String Conversion in Rust Source: https://github.com/hack-ink/array-bytes/blob/main/audit-report.md This Rust code snippet shows an unsafe conversion of byte vectors to strings using `String::from_utf8_unchecked`. This method bypasses UTF-8 validation, requiring prior assurance that the byte vector contains valid UTF-8 data to avoid undefined behavior. This technique is used in `src/hex/hexify.rs` after validation. ```rust unsafe { String::from_utf8_unchecked(hex_bytes.into_vec()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.