o3/buffer/storage/shared/
str.rs1use std::{ops, str};
2
3use crate::{
4 buffer,
5 buffer::{storage, storage::regions},
6};
7
8#[repr(transparent)]
10pub struct Str<O: buffer::raw::Source = storage::Shared> {
11 text: buffer::raw::Utf8<O>,
12}
13
14impl<O: buffer::raw::Source> Str<O> {
15 pub fn from_utf8(owner: O) -> Result<Self, str::Utf8Error> {
16 Ok(Self {
17 text: buffer::raw::Utf8::try_new(owner)?,
18 })
19 }
20}
21
22impl<O: buffer::raw::Source> ops::Deref for Str<O> {
23 type Target = buffer::raw::Utf8<O>;
24 fn deref(&self) -> &Self::Target {
25 &self.text
26 }
27}
28
29impl TryFrom<&mut String> for Str {
30 type Error = crate::alloc::Exhausted;
31
32 fn try_from(value: &mut String) -> Result<Self, Self::Error> {
34 let owner = storage::Shared {
35 region: regions::Header::try_new(value)?,
36 };
37 Ok(Self {
39 text: unsafe { buffer::raw::Utf8::new(owner) },
40 })
41 }
42}
43
44const _: () = {
45 assert!(size_of::<Str>() == size_of::<storage::Shared>());
46 assert!(size_of::<Result<Str, crate::alloc::Exhausted>>() == size_of::<Str>());
47};