add readonly roc list and roc str to roc_std

This commit is contained in:
Brendan Hansknecht 2024-12-10 20:03:08 -08:00
parent 0e162a767d
commit 98cde41439
No known key found for this signature in database
GPG key ID: A199D0660F95F948
3 changed files with 105 additions and 3 deletions

View file

@ -824,6 +824,50 @@ impl RocRefcounted for RocStr {
}
}
#[repr(transparent)]
pub struct ReadOnlyRocStr(RocStr);
unsafe impl Send for ReadOnlyRocStr {}
unsafe impl Sync for ReadOnlyRocStr {}
impl RocRefcounted for ReadOnlyRocStr {
fn inc(&mut self) {
}
fn dec(&mut self) {
}
fn is_refcounted() -> bool {
true
}
}
impl Clone for ReadOnlyRocStr {
fn clone(&self) -> Self {
ReadOnlyRocStr(self.0.clone())
}
}
impl From<RocStr> for ReadOnlyRocStr {
fn from(mut s: RocStr) -> Self {
if s.is_unique() {
unsafe {s.set_readonly()};
}
if s.is_readonly() {
ReadOnlyRocStr(s)
} else {
// This is not readonly or unique, do a deep copy.
ReadOnlyRocStr::from(RocStr::from(s.as_str()))
}
}
}
impl From<ReadOnlyRocStr> for RocStr {
fn from(s: ReadOnlyRocStr) -> Self {
s.0
}
}
#[repr(C)]
struct BigString {
elements: NonNull<u8>,