Skip to content

Commit

Permalink
chore: add safety doc comments to ustr functions
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Nov 30, 2024
1 parent 92d3464 commit 8bda911
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions asm-lsp/ustr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
//! input unnecessarily and from my survey our inputs are mostly ascii
use std::str;

/// Converts a byte slice to a string slice without checking for valid UTF-8.
///
/// # Safety
///
/// The caller must ensure that the input slice is valid UTF-8. Passing invalid UTF-8
/// to this function invokes undefined behavior.
#[must_use]
pub const fn get_str(v: &[u8]) -> &str {
unsafe { str::from_utf8_unchecked(v) }
}

/// Converts a byte vector to a string without checking for valid UTF-8.
///
/// # Safety
///
/// The caller must ensure that the input vector is valid UTF-8. Passing invalid UTF-8
/// to this function invokes undefined behavior.
#[must_use]
pub fn get_string(v: Vec<u8>) -> String {
unsafe { String::from_utf8_unchecked(v) }
Expand Down

0 comments on commit 8bda911

Please sign in to comment.