summaryrefslogtreecommitdiff
path: root/cli/util/text_encoding.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/util/text_encoding.rs')
-rw-r--r--cli/util/text_encoding.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/cli/util/text_encoding.rs b/cli/util/text_encoding.rs
index 0b7601cb9..df72cc2be 100644
--- a/cli/util/text_encoding.rs
+++ b/cli/util/text_encoding.rs
@@ -103,6 +103,21 @@ pub fn arc_str_to_bytes(arc_str: Arc<str>) -> Arc<[u8]> {
unsafe { Arc::from_raw(raw as *const [u8]) }
}
+/// Converts an `Arc<u8>` to an `Arc<str>` if able.
+#[allow(dead_code)]
+pub fn arc_u8_to_arc_str(
+ arc_u8: Arc<[u8]>,
+) -> Result<Arc<str>, std::str::Utf8Error> {
+ // Check that the string is valid UTF-8.
+ std::str::from_utf8(&arc_u8)?;
+ // SAFETY: the string is valid UTF-8, and the layout Arc<[u8]> is the same as
+ // Arc<str>. This is proven by the From<Arc<str>> impl for Arc<[u8]> from the
+ // standard library.
+ Ok(unsafe {
+ std::mem::transmute::<std::sync::Arc<[u8]>, std::sync::Arc<str>>(arc_u8)
+ })
+}
+
#[cfg(test)]
mod tests {
use std::sync::Arc;