Skip to content

Commit

Permalink
Add impl Default for Cow (#32)
Browse files Browse the repository at this point in the history
* Add impl Default for Cow

* Fix type inference on &[]
  • Loading branch information
maciejhirsz authored May 2, 2020
1 parent 050191f commit 856988b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "beef"
version = "0.4.3"
version = "0.4.4"
authors = ["Maciej Hirsz <hello@maciej.codes>"]
edition = "2018"
description = "More compact Cow"
Expand Down
12 changes: 12 additions & 0 deletions src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ where
}
}

impl<'a, T, U> Default for Cow<'a, T, U>
where
T: Beef + ?Sized,
U: Capacity,
&'a T: Default,
{
#[inline]
fn default() -> Self {
Cow::borrowed(Default::default())
}
}

impl<T, U> Eq for Cow<'_, T, U>
where
T: Eq + Beef + ?Sized,
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ macro_rules! test { ($tmod:ident => $cow:path) => {

assert_eq!(&*FOO, b"bar");
}

#[test]
fn default_str() {
let empty: Cow<str> = Default::default();

assert_eq!(&*empty, "");
}

#[test]
fn default_slice() {
let empty: Cow<[u8]> = Default::default();

assert_eq!(&*empty, b"");
}
}
} }

Expand Down

0 comments on commit 856988b

Please sign in to comment.