Skip to content

How to make a blanket impl MyTrait for T: FunctorMut<'a, A>? #1

@Expurple

Description

@Expurple

I tried to implement another trait (Sanitize) for all FunctorMut's where the item implements Sanitize. But when I try to do this:

use fmap::FunctorMut;

/// The Sanitize trait generalises types that are to be sanitized.
pub trait Sanitize {
    /// Call this associated method when sanitizing.
    fn sanitize(&mut self);
}

/// A blanket impl that allows sanitizing values inside of common data structures like [Option] and [Vec].
///
/// ## Example
///
/// ```
/// use sanitizer::Sanitize;
///
/// #[derive(Debug, PartialEq)]
/// struct MyValue(i32);
///
/// impl Sanitize for MyValue {
///     /// If the inner value is `0`, change it to `1`.
///     fn sanitize(&mut self) {
///         if self.0 == 0 {
///             self.0 = 1;
///         }
///     }
/// }
///
/// let mut wrapped_value = Some(MyValue(0));
/// wrapped_value.sanitize();
/// assert_eq!(wrapped_value, Some(MyValue(1)));
/// ```
impl<'a, A, T> Sanitize for T
where
    A: 'a,
    T: FunctorMut<'a, A>,
    T::Inner: Sanitize,
{
    fn sanitize(&mut self) {
        self.fmap_mut(Sanitize::sanitize)
    }
}

I get this error:

error[E0207]: the type parameter `A` is not constrained by the impl trait, self type, or predicates

and I don't know how to proceed.

You can find context and details here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions