Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions bounded-collections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ impl<T: Default> Get<T> for () {
}
}

/// Converts [`Get<I>`] to [`Get<R>`] using [`Into`].
///
/// Acts as a type-safe bridge between `Get` implementations where `I: Into<R>`.
///
/// - `Inner`: The [`Get<I>`] implementation
/// - `I`: Source type to convert from
/// - `R`: Target type to convert into
///
/// # Example
/// ```
/// use bounded_collections::Get;
/// use bounded_collections::GetInto;
///
/// struct MyGetter;
/// impl Get<u16> for MyGetter { fn get() -> u16 { 42 } }
/// assert_eq!(GetInto::<MyGetter, u16, u32>::get(), 42u32);
/// ```
pub struct GetInto<Inner, I, R>(core::marker::PhantomData<(Inner, I, R)>);

impl<Inner, I, R> Get<R> for GetInto<Inner, I, R>
where
Inner: Get<I>,
I: Into<R>,
{
/// 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<T: Default> Get<T> for GetDefault {
Expand Down
Loading