diff --git a/bounded-collections/src/lib.rs b/bounded-collections/src/lib.rs index c7d5d1f7..182749ad 100644 --- a/bounded-collections/src/lib.rs +++ b/bounded-collections/src/lib.rs @@ -51,6 +51,38 @@ impl Get for () { } } +/// Converts [`Get`] to [`Get`] using [`Into`]. +/// +/// Acts as a type-safe bridge between `Get` implementations where `I: Into`. +/// +/// - `Inner`: The [`Get`] implementation +/// - `I`: Source type to convert from +/// +/// # Example +/// ``` +/// use bounded_collections::Get; +/// use bounded_collections::GetInto; +/// +/// struct MyGetter; +/// impl Get for MyGetter { fn get() -> u16 { 42 } } +/// let foo: u32 = GetInto::::get(); +/// assert_eq!(foo, 42u32); // <--- infered as u32 +/// ``` +pub struct GetInto(core::marker::PhantomData<(Inner, I)>); + +impl Get for GetInto +where + Inner: Get, + I: Into, +{ + /// Returns the converted value by: + /// 1. Getting the inner value of type `I` + /// 2. Converting it to type `R` using [`Into`] + fn get() -> R { + Inner::get().into() + } +} + /// Implement Get by returning Default for any type that implements Default. pub struct GetDefault; impl Get for GetDefault {