2

Is it possible to define a trait like the following one:

pub trait DataValidator {
  fn validate_data(&self, data: TBD) -> bool;
}

where the TBD type can be different data types like u32, Vec<u8>, Vec<Vec<u8>>, ... and have a different implementation for different enums that implement that trait?

The idea is to create a validator for different data types. I have the following enum:

pub enum DataType {
  Text,
  Number,
  Array,
}

and I have an struct that uses that enum

pub struct Validator {
  pub data_type: DataType
}

I want to implement the trait for Validator, but the input that the validate_data function is not the same all the time, or at least that's what I want to cover.

So for example, let's say I have a Validator name validator with data_type set as Text. I want to then call the validate_data function like validator.validate_data(data) where data is a Vec<u8>. It only has to validate that the length of the Vec is within a certain limits.

But let's say I have another Validator named validator2 with data_type set as Array. I want to pass to the validate_data function an array of different DataTypes and validate each one of them using the same logic, but for that I need the trait to be flexible with the input type. Is it possible?

3
  • 1
    I think you would need to do something like this? Commented Nov 10, 2022 at 15:12
  • Although I've simplified here the example, I'm already working with structs within the enum, but the problem is that the trait itself receives an additional parameter that can be anything, so the problem here is how to set that data input to any data type Commented Nov 10, 2022 at 15:18
  • 1
    I think you'd want to make the trait generic over the type its validating. Like impl DataValidator<u32> for Validator means that it can validate u32s and do that for all the types you need. Commented Nov 10, 2022 at 15:40

1 Answer 1

2

You can implement dummy trait SupportedDataType just to indicate types that you support and connect each DataValidator trait with some type (ValidatedDataType) which will be trait SupportedDataType then you can change type for every implementation (which as I understood is required feature).

trait SupportedDataType {}

impl SupportedDataType for String {}

impl SupportedDataType for i32 {}

pub trait DataValidator {
    type ValidatedDataType: SupportedDataType;

    fn validate_data(&self, data: Self::ValidatedDataType) -> bool;
}

struct Validator {}

impl DataValidator for Validator {
    type ValidatedDataType = u64;

    fn validate_data(&self, data: Self::ValidatedDataType) -> bool {
        return true;
    }
}

In specific implementation you need to provide specific type.

Sign up to request clarification or add additional context in comments.

1 Comment

It looks good to me! I’ll test it out and accept the answer asap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.