Skip to content

Commit

Permalink
types: add String::to_str()
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 3, 2024
1 parent 3e7ca96 commit b566d7c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/types/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use alloc::borrow::Cow;
use alloc::string::String as StdString;
use core::str::{self, Utf8Error};
use core::{ffi, slice};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -106,6 +107,12 @@ impl String {
NonOwning::new(Self { ..*self })
}

/// Yields a string slice if the [`String`]'s contents are valid UTF-8.
#[inline]
pub fn to_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.as_bytes())
}

/// Converts the `String` into Rust's `std::string::String`. If it already
/// holds a valid UTF-8 byte sequence no allocation is made. If it doesn't
/// the `String` is copied and all invalid sequences are replaced with `�`.
Expand Down

0 comments on commit b566d7c

Please sign in to comment.