Skip to content

Commit ebd4ba0

Browse files
authored
Merge pull request #26 from troiganto/master
Allow more conversions and impl Default
2 parents 34e17ca + 29b30b3 commit ebd4ba0

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/ascii.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ impl<S> Ascii<S> {
1414
pub fn new(s: S) -> Ascii<S> {
1515
Ascii(s)
1616
}
17+
18+
#[inline]
19+
pub fn into_inner(self) -> S {
20+
self.0
21+
}
1722
}
1823

1924
impl<S> Deref for Ascii<S> {

src/lib.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#[cfg(feature = "nightly")]
4646
extern crate test;
4747

48+
use std::borrow::Cow;
4849
#[cfg(__unicase__iter_cmp)]
4950
use std::cmp::Ordering;
5051
use std::fmt;
@@ -62,7 +63,7 @@ mod unicode;
6263
pub struct UniCase<S>(Encoding<S>);
6364

6465
/// Case Insensitive wrapper of Ascii strings.
65-
#[derive(Clone, Copy, Debug)]
66+
#[derive(Clone, Copy, Debug, Default)]
6667
pub struct Ascii<S>(S);
6768

6869
/// Compare two string-like types for case-less equality, using unicode folding.
@@ -106,6 +107,12 @@ macro_rules! inner {
106107
});
107108
}
108109

110+
impl<S: AsRef<str> + Default> Default for UniCase<S> {
111+
fn default() -> Self {
112+
Self::new(Default::default())
113+
}
114+
}
115+
109116
impl<S: AsRef<str>> UniCase<S> {
110117
/// Creates a new `UniCase`.
111118
///
@@ -126,6 +133,17 @@ impl<S: AsRef<str>> UniCase<S> {
126133
}
127134
}
128135

136+
impl<S> UniCase<S> {
137+
/// Unwraps the inner value held by this `UniCase`.
138+
#[inline]
139+
pub fn into_inner(self) -> S {
140+
match self.0 {
141+
Encoding::Ascii(s) => s.0,
142+
Encoding::Unicode(s) => s.0,
143+
}
144+
}
145+
}
146+
129147
impl<S> Deref for UniCase<S> {
130148
type Target = S;
131149
#[inline]
@@ -203,22 +221,27 @@ macro_rules! into_impl {
203221
($to:ty) => (
204222
impl<'a> Into<$to> for UniCase<$to> {
205223
fn into(self) -> $to {
206-
match self.0 {
207-
Encoding::Ascii(Ascii(s)) => s,
208-
Encoding::Unicode(Unicode(s)) => s,
209-
}
224+
self.into_inner()
210225
}
211226
}
212227
);
213228
}
214229

215-
from_impl!(&'a str => &'a str);
230+
impl<S: AsRef<str>> From<S> for UniCase<S> {
231+
fn from(s: S) -> Self {
232+
UniCase::unicode(s)
233+
}
234+
}
235+
236+
from_impl!(&'a str => Cow<'a, str>);
237+
from_impl!(String => Cow<'a, str>);
216238
from_impl!(&'a str => String);
239+
from_impl!(Cow<'a, str> => String; into_owned);
217240
from_impl!(&'a String => &'a str; as_ref);
218-
from_impl!(String => String);
219241

220242
into_impl!(&'a str);
221243
into_impl!(String);
244+
into_impl!(Cow<'a, str>);
222245

223246
#[cfg(__unicase__iter_cmp)]
224247
impl<T: AsRef<str>> PartialOrd for UniCase<T> {
@@ -306,7 +329,7 @@ mod tests {
306329
#[cfg(feature = "nightly")]
307330
#[inline(never)]
308331
fn is_ascii(bytes: &[u8]) -> bool {
309-
#[allow(unused)]
332+
#[allow(unused, deprecated)]
310333
use std::ascii::AsciiExt;
311334
bytes.is_ascii()
312335
}

src/unicode/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher};
55
use self::map::lookup;
66
mod map;
77

8-
#[derive(Clone, Copy, Debug)]
8+
#[derive(Clone, Copy, Debug, Default)]
99
pub struct Unicode<S>(pub S);
1010

1111
impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<Unicode<S2>> for Unicode<S1> {

0 commit comments

Comments
 (0)