into-ref 0.1.2

Generic assist traits, consume self into a reference
Documentation
  • Coverage
  • 83.33%
    5 out of 6 items documented1 out of 5 items with examples
  • Size
  • Source code size: 5.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 46.9 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 1s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • A4-Tacks/into-ref-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • A4-Tacks

Generic assist traits, consume self into a reference

| Input | Into<&T> | Into<&mut T> | IntoRef<Out = T> | IntoRefMut<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