Misc RocStr fixes

Add get_small_str_ptr_mut.
Simply debug message.
Just use slices in PartialEq for simplicity.
This commit is contained in:
Brendan Hansknecht 2020-10-12 16:59:55 -07:00
parent af02e8e359
commit 641a183c26

View file

@ -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,13 +380,6 @@ 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: true, elements: [ 1,2,3,4] }
f.debug_struct("RocStr")
.field("is_small_str", &true)
.field("elements", &self.as_slice())
.finish()
} else {
// RocStr { is_small_str: false, storage: Refcounted(3), elements: [ 1,2,3,4] } // RocStr { is_small_str: false, storage: Refcounted(3), elements: [ 1,2,3,4] }
f.debug_struct("RocStr") f.debug_struct("RocStr")
.field("is_small_str", &false) .field("is_small_str", &false)
@ -390,24 +387,11 @@ impl fmt::Debug for RocStr {
.field("elements", &self.as_slice()) .field("elements", &self.as_slice())
.finish() .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
} }
} }