Is it possible to check the type of compile time in growth in Rust?

I do not want to check whether a type has a certain attribute, but I would like to be able to distinguish, for example. struct and integer. Since both struct and integer can implement the same attribute, I do not know how I could tell them separately.

The reason I want to do this is because I use serde_json to convert a generic type to JSON, but I only want it to become JSON Object(what happens when it's struct), but it should not convert to anything other (e.g. JSON I64). Since both structures and integers can implement a trait Serialize, there is no way to talk about it.

I am currently letting the process panic because it is not an error that can cause it to recover, but since I could find out at compile time, I wonder if there is any mechanism for determining the type at compile time.

I would like to know how I can distinguish types according to their "goodness", and not according to their characteristics.

+4
source share
1 answer

Even if you manage to compare types at compile time, nothing prevents structserializing like Json::I64. Implementation Serializecan be any! I can come up with some partial solutions:

Check execution

, , Json::Object . , , . , , .

:

trait SerializeAsObject : Serialize {}

, , , . i64, .

:

, , , , , . , , , ( Idris, ).

, . , . , , , Serialize .

, , ! , , , , , , , , .

+2

Source: https://habr.com/ru/post/1664753/


All Articles