Skip to content

Commit

Permalink
string: add impl From<&IString> for IString (yewstack#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Nov 28, 2023
1 parent 0dfe6d5 commit 87da5e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ impl<T: ImplicitClone + 'static> From<Rc<[T]>> for IArray<T> {
}
}

impl<T: ImplicitClone + 'static> From<&IArray<T>> for IArray<T> {
fn from(a: &IArray<T>) -> IArray<T> {
a.clone()
}
}

impl<T: ImplicitClone + 'static> IArray<T> {
/// Returns an iterator over the slice.
///
Expand Down Expand Up @@ -281,4 +287,10 @@ mod test_array {
const _ARRAY_F32: IArray<f32> = IArray::Static(&[]);
const _ARRAY_F64: IArray<f64> = IArray::Static(&[]);
}

#[test]
fn from() {
let x: IArray<u32> = IArray::Static(&[]);
let _out = IArray::from(&x);
}
}
14 changes: 14 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'sta
}
}

impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
From<&IMap<K, V>> for IMap<K, V>
{
fn from(a: &IMap<K, V>) -> IMap<K, V> {
a.clone()
}
}

impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static> IMap<K, V> {
/// Return an iterator over the key-value pairs of the map, in their order.
#[inline]
Expand Down Expand Up @@ -425,4 +433,10 @@ mod test_map {
const _MAP_F32: IMap<u32, f32> = IMap::Static(&[]);
const _MAP_F64: IMap<u32, f64> = IMap::Static(&[]);
}

#[test]
fn from() {
let x: IMap<u32, u32> = IMap::Static(&[]);
let _out = IMap::from(&x);
}
}
12 changes: 12 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ impl From<Cow<'static, str>> for IString {
}
}

impl From<&IString> for IString {
fn from(s: &IString) -> IString {
s.clone()
}
}

macro_rules! impl_cmp_as_str {
(PartialEq::<$type1:ty, $type2:ty>) => {
impl_cmp_as_str!(PartialEq::<$type1, $type2>::eq -> bool);
Expand Down Expand Up @@ -394,4 +400,10 @@ mod test_string {
// this assert exists to ensure the cow lives after the strong_count assert
assert_eq!(cow, "foo");
}

#[test]
fn from_ref() {
let s = IString::Static("foo");
let _out = IString::from(&s);
}
}

0 comments on commit 87da5e4

Please sign in to comment.