| 1 |
use core::str::Utf8Error; |
| 2 |
|
| 3 |
use bytes::Buf; |
| 4 |
|
| 5 |
use crate::{CompactString, Repr}; |
| 6 |
|
| 7 |
impl CompactString { |
| 8 |
/// Converts a buffer of bytes to a [`CompactString`] |
| 9 |
/// |
| 10 |
/// # Examples |
| 11 |
/// ### Basic usage |
| 12 |
/// ``` |
| 13 |
/// # use compact_str::CompactString; |
| 14 |
/// # use std::collections::VecDeque; |
| 15 |
/// |
| 16 |
/// // `bytes::Buf` is implemented for `VecDeque<u8>` |
| 17 |
/// let mut sparkle_heart = VecDeque::from(vec![240, 159, 146, 150]); |
| 18 |
/// // We know these bytes are valid, so we can `.unwrap()` or `.expect(...)` here |
| 19 |
/// let compact_str = CompactString::from_utf8_buf(&mut sparkle_heart).expect("valid utf-8"); |
| 20 |
/// |
| 21 |
/// assert_eq!(compact_str, "💖"); |
| 22 |
/// ``` |
| 23 |
/// |
| 24 |
/// ### With invalid/non-UTF8 bytes |
| 25 |
/// ``` |
| 26 |
/// # use compact_str::CompactString; |
| 27 |
/// # use std::io; |
| 28 |
/// |
| 29 |
/// // `bytes::Buf` is implemented for `std::io::Cursor<&[u8]>` |
| 30 |
/// let mut invalid = io::Cursor::new(&[0, 159]); |
| 31 |
/// |
| 32 |
/// // The provided buffer is invalid, so trying to create a `CompactString` will fail |
| 33 |
/// assert!(CompactString::from_utf8_buf(&mut invalid).is_err()); |
| 34 |
/// ``` |
| 35 |
#[cfg_attr(docsrs, doc(cfg(feature = "bytes")))] |
| 36 |
pub fn from_utf8_buf<B: Buf>(buf: &mut B) -> Result<Self, Utf8Error> { |
| 37 |
Repr::from_utf8_buf(buf).map(CompactString) |
| 38 |
} |
| 39 |
|
| 40 |
/// Converts a buffer of bytes to a [`CompactString`], without checking that the provided buffer |
| 41 |
/// is valid UTF-8. |
| 42 |
/// |
| 43 |
/// # Safety |
| 44 |
/// This function is unsafe because it does not check that the provided bytes are valid UTF-8. |
| 45 |
/// If this constraint is violated, it may cause memory safety issues with futures uses of the |
| 46 |
/// `CompactString`, as the rest of the library assumes that `CompactString`s are valid UTF-8 |
| 47 |
/// |
| 48 |
/// # Examples |
| 49 |
/// ``` |
| 50 |
/// # use compact_str::CompactString; |
| 51 |
/// # use std::io; |
| 52 |
/// |
| 53 |
/// let word = "hello world"; |
| 54 |
/// // `bytes::Buf` is implemented for `std::io::Cursor<&[u8]>` |
| 55 |
/// let mut buffer = io::Cursor::new(word.as_bytes()); |
| 56 |
/// let compact_str = unsafe { CompactString::from_utf8_buf_unchecked(&mut buffer) }; |
| 57 |
/// |
| 58 |
/// assert_eq!(compact_str, word); |
| 59 |
/// ``` |
| 60 |
#[cfg_attr(docsrs, doc(cfg(feature = "bytes")))] |
| 61 |
pub unsafe fn from_utf8_buf_unchecked<B: Buf>(buf: &mut B) -> Self { |
| 62 |
let repr = Repr::from_utf8_buf_unchecked(buf); |
| 63 |
CompactString(repr) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[cfg(test)] |
| 68 |
mod test { |
| 69 |
use alloc::string::String; |
| 70 |
use alloc::vec::Vec; |
| 71 |
#[cfg(feature = "std")] |
| 72 |
use std::io::Cursor; |
| 73 |
|
| 74 |
use proptest::prelude::*; |
| 75 |
use test_strategy::proptest; |
| 76 |
|
| 77 |
use crate::tests::{rand_bytes, rand_unicode}; |
| 78 |
use crate::CompactString; |
| 79 |
|
| 80 |
const MAX_SIZE: usize = core::mem::size_of::<String>(); |
| 81 |
|
| 82 |
#[proptest] |
| 83 |
#[cfg_attr(miri, ignore)] |
| 84 |
fn proptest_buffers_roundtrip(#[strategy(rand_unicode())] word: String) { |
| 85 |
let mut buf = Cursor::new(word.as_bytes()); |
| 86 |
let compact = CompactString::from_utf8_buf(&mut buf).unwrap(); |
| 87 |
|
| 88 |
proptest::prop_assert_eq!(&word, &compact); |
| 89 |
} |
| 90 |
|
| 91 |
#[proptest] |
| 92 |
#[cfg_attr(miri, ignore)] |
| 93 |
fn proptest_allocated_properly(#[strategy(rand_unicode())] word: String) { |
| 94 |
let mut buf = Cursor::new(word.as_bytes()); |
| 95 |
let compact = CompactString::from_utf8_buf(&mut buf).unwrap(); |
| 96 |
|
| 97 |
if word.len() <= MAX_SIZE { |
| 98 |
proptest::prop_assert!(!compact.is_heap_allocated()) |
| 99 |
} else { |
| 100 |
proptest::prop_assert!(compact.is_heap_allocated()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[proptest] |
| 105 |
#[cfg_attr(miri, ignore)] |
| 106 |
fn proptest_only_accept_valid_utf8(#[strategy(rand_bytes())] bytes: Vec<u8>) { |
| 107 |
let mut buf = Cursor::new(bytes.as_slice()); |
| 108 |
|
| 109 |
let compact_result = CompactString::from_utf8_buf(&mut buf); |
| 110 |
let str_result = core::str::from_utf8(bytes.as_slice()); |
| 111 |
|
| 112 |
match (compact_result, str_result) { |
| 113 |
(Ok(c), Ok(s)) => prop_assert_eq!(c, s), |
| 114 |
(Err(c_err), Err(s_err)) => prop_assert_eq!(c_err, s_err), |
| 115 |
_ => panic!("CompactString and core::str read UTF-8 differently?"), |
| 116 |
} |
| 117 |
} |
| 118 |
} |