nutype icon indicating copy to clipboard operation
nutype copied to clipboard

Relax the rules around derive

Open musjj opened this issue 1 year ago β€’ 2 comments

It looks like that nutype has its own special derive() implementation and it can really get in the way sometimes. For example, I want to use the sqlx::Type derive, but I can't:

#[nutype(
    validate(not_empty, len_char_max = 20),
    derive(sqlx::Type), // #[nutype] does not know how to derive `sqlx` trait.
    sqlx(transparent)
)]
pub struct Name(String);

There's also issues regarding conditional derives: https://github.com/greyblake/nutype/issues/188

Would it be possible to relax the rule and just let the user use derive directly on the type?

musjj avatar Oct 23 '24 19:10 musjj

Hi, thanks for reporting the problem. Nutype takes a full control over derive(..) to ensure that it's not possible to derive traits that possibly can mutate value avoiding the constraints. E.g. deriving DerefMut would make a problem.

I have plans to have in-house support for Diesel and SQLx.

Would it be possible to relax the rule and just let the user use derive directly on the type? Also I have plan to add a feature like unsafe-derive, which would allow users to derive whatever they want.

greyblake avatar Oct 24 '24 08:10 greyblake

Deriving sqlx::Type is possible now with unsafe_derive() in 0.6.2:

#[nutype(
    validate(not_empty, len_char_max = 20),
    derive_unsafe(sqlx::Type),
    sqlx(transparent) // However, there is no way to pass this attribute yet 
)]
pub struct Name(String);

greyblake avatar Jul 30 '25 06:07 greyblake