-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define TypeDef for types like uuid::Uuid
?
#28
Comments
As the Rust error code is indicating, you are running into a common constraint in Rust called the "orphan rule". You can't implement a foreign trait for a foreign type, because there's nothing preventing a different crate from doing the same thing and defining a conflicting implementation. A common way to get around this is to create a local "newtype" wrapper around the type and use this instead. This is the approach I take in most of my projects, as it gives you a lot of control over other aspects of your type as well. #[derive(Debug, Serialize, Deserialize)]
pub struct MyId(pub uuid::Uuid);
impl TypeDef for MyId {
const INFO: TypeInfo = TypeInfo::Native(NativeTypeInfo {
r#ref: TypeExpr::ident(Ident("string")),
});
} Another approach you can use is to tell the #[derive(Serialize, Deserialize, TypeDef)]
pub struct MyStruct {
#[type_def(type_of = "String")]
pub id: Uuid;
pub foo: usize;
} You can also combine the two if you want to use a newtype but don't want to implement #[derive(Debug, Serialize, Deserialize, TypeDef)]
pub struct MyId(#[type_def(type_of = "String")] pub uuid::Uuid); |
Thank you so much, @dbeckwith ! |
Hello!
I tried to do something like this:
But it shows:
The text was updated successfully, but these errors were encountered: