mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Misc RocStr fixes
Add get_small_str_ptr_mut. Simply debug message. Just use slices in PartialEq for simplicity.
This commit is contained in:
parent
af02e8e359
commit
641a183c26
1 changed files with 13 additions and 29 deletions
|
@ -314,11 +314,15 @@ impl RocStr {
|
||||||
(self as *const RocStr).cast()
|
(self as *const RocStr).cast()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_small_str_ptr_mut(&mut self) -> *mut u8 {
|
||||||
|
(self as *mut RocStr).cast()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_slice_with_capacity(slice: &[u8], capacity: usize) -> RocStr {
|
pub fn from_slice_with_capacity(slice: &[u8], capacity: usize) -> RocStr {
|
||||||
assert!(slice.len() <= capacity);
|
assert!(slice.len() <= capacity);
|
||||||
if capacity < core::mem::size_of::<usize>() {
|
if capacity < core::mem::size_of::<usize>() {
|
||||||
let rocstr = RocStr::empty();
|
let mut rocstr = RocStr::empty();
|
||||||
let target_ptr = rocstr.get_small_str_ptr() as *mut u8;
|
let target_ptr = rocstr.get_small_str_ptr_mut();
|
||||||
let source_ptr = slice.as_ptr() as *const u8;
|
let source_ptr = slice.as_ptr() as *const u8;
|
||||||
for index in 0..(slice.len() as isize) {
|
for index in 0..(slice.len() as isize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -376,38 +380,18 @@ impl RocStr {
|
||||||
|
|
||||||
impl fmt::Debug for RocStr {
|
impl fmt::Debug for RocStr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
if self.is_small_str() {
|
// RocStr { is_small_str: false, storage: Refcounted(3), elements: [ 1,2,3,4] }
|
||||||
// RocStr { is_small_str: true, elements: [ 1,2,3,4] }
|
f.debug_struct("RocStr")
|
||||||
f.debug_struct("RocStr")
|
.field("is_small_str", &false)
|
||||||
.field("is_small_str", &true)
|
.field("storage", &self.storage())
|
||||||
.field("elements", &self.as_slice())
|
.field("elements", &self.as_slice())
|
||||||
.finish()
|
.finish()
|
||||||
} else {
|
|
||||||
// RocStr { is_small_str: false, storage: Refcounted(3), elements: [ 1,2,3,4] }
|
|
||||||
f.debug_struct("RocStr")
|
|
||||||
.field("is_small_str", &false)
|
|
||||||
.field("storage", &self.storage())
|
|
||||||
.field("elements", &self.as_slice())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for RocStr {
|
impl PartialEq for RocStr {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
if self.length != other.length {
|
self.as_slice() == other.as_slice()
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..(self.length as isize) {
|
|
||||||
unsafe {
|
|
||||||
if *self.elements.offset(i) != *other.elements.offset(i) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue