Skip to content

Commit

Permalink
fix bug caused by empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Feb 13, 2023
1 parent 821077e commit f1e5bd2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ impl<'a> From<&'a str> for MownStr<'a> {
}

impl<'a> From<Box<str>> for MownStr<'a> {
fn from(other: Box<str>) -> MownStr<'a> {
fn from(mut other: Box<str>) -> MownStr<'a> {
let len = other.len();
assert!(len <= LEN_MASK);
let addr = NonNull::from(&other.as_bytes()[0]);
let addr = other.as_mut_ptr();
let addr = unsafe {
// SAFETY: ptr can not be null,
NonNull::new_unchecked(addr)
};

std::mem::forget(other);

Expand Down Expand Up @@ -502,6 +506,12 @@ mod test {
assert!(increase < 3.5);
}

#[test]
fn empty_string() {
let empty = "".to_string();
let _ = MownStr::from(empty);
}

const CAP: usize = 100_000_000;

fn get_vmsize() -> usize {
Expand Down

0 comments on commit f1e5bd2

Please sign in to comment.