Open romanoji opened 1 year ago
Looking into this
@romanoji I can add a implementation for Vector, the code generated is different for each data structure, so I don't think we can support this for every generic T<X>
@Daksh14, sounds good to me. :+1:
If you could handle it also for Option<T>
that'd be awesome. I suppose it'd be sufficient for most of the cases.
We are running into the same Problem. @romanoji what workaround did you use? @Daksh14 anything we can help in implementing this?
@misl-smlz, as I mentioned in my initial comment, I dropped #[sanitize]
annotations from the top-level struct (for Vec<T>
and Option<T>
, except for Option<String>
) and run sanitization for each of those attributes manually. I don't remember all the details right now but that was the most straightforward solution.
I can add a implementation for Vector, the code generated is different for each data structure, so I don't think we can support this for every generic
T<X>
You can be generic over something like fmap::FunctorMut
- an existing functor abstraction that's already implemented for the standard data structures, including Option
and Vec
. I think, this is the best way to approach this.
I can add a implementation for Vector, the code generated is different for each data structure, so I don't think we can support this for every generic
T<X>
You can be generic over something like
fmap::FunctorMut
- an existing functor abstraction that's already implemented for the standard data structures, includingOption
andVec
. I think, this is the best way to approach this.
Sorry for being inactive, The library uses syn
so what type would that correspond to there?
I would also have to update the syn version because we use 1.x and 2.x is out
My initial idea was about providing a blanket impl Sanitize
in sanitizer
, without changing the code generated by the proc macro in dependent crates:
/// 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)
}
}
But I tried making a PR and I get
error[E0207]: the type parameter `A` is not constrained by the impl trait, self type, or predicates
which I'm not sure how to resolve.
If we won't be able to resolve this, then I'd just provide similar generic impls for Option
and Vec
directly: #15
Hello maintainers! 👋 The crate seems to lack of support for
Option<T>
, whereT
is a struct or an enum and forVec<T>
.Currently, it's not possible to
sanitize
a struct as a whole which relies on generic types other thanOption<T>
, withT
as a primitive.Here's an example:
When running such code it fails with the following errors:
I came up with a simple workaround for now, but it's quite bothersome to handle for more complex structs. It also requires to drop
#[sanitize]
proc macro to make it work. Here's a rough idea, based on an example shared above:Based on my initial research it seems that handling
Option<T>
for structs/enums might be not that hard to implement. However, I'm not so sure aboutVec<T>
(and perhaps other iterables too).Looking forward to hear your thoughts about it. 🙂