Skip to main content

Crate into_ref

Crate into_ref 

Source
Expand description

Generic assist traits, consume self into a reference

| Input | Into<&T> | Into<&mut T> | ThisRef<Out = T> | ThisMut<Out = T> | | &T | pass | fail | pass | fail | | &mut T | fail | pass | pass | pass |

§Examples

AsRef/AsMut cannot using in owned convert

ⓘ
fn foo<'a>(i: impl IntoIterator<Item: 'a + AsRef<u32>>) -> Vec<&'a u32> {
    i.into_iter().map(|it| it.as_ref()).collect()
}
fn bar() { foo([&2u32]); } // compile_fail
fn baz() { foo([&mut 2u32]); } // compile_fail

error[E0515]: cannot return value referencing function parameter it


Use Into to instead, cannot convert &mut T -> &T:

ⓘ
fn foo<'a>(i: impl IntoIterator<Item: Into<&'a u32>>) -> Vec<&'a u32> {
    i.into_iter().map(Into::into).collect()
}
fn bar() { foo([&2u32]); } // pass
fn baz() { foo([&mut 2u32]); } // compile_fail

error[E0277]: the trait bound &u32: From<&mut u32> is not satisfied


Use IntoRef to instead:

use into_ref::IntoRef;
fn foo<'a>(i: impl IntoIterator<Item: IntoRef<'a, Out = u32>>) -> Vec<&'a u32> {
    i.into_iter().map(IntoRef::into_ref).collect()
}
fn bar() { foo([&2u32]); } // pass
fn baz() { foo([&mut 2u32]); } // pass

Traits§

IntoRef
Consume self into reference
IntoRefMut
Consume self into mutable reference