gecko-dev/third_party/rust/ron/docs/extensions.md

1.3 KiB

RON extensions

RON has extensions that can be enabled by adding the following attribute at the top of your RON document:

#![enable(...)]

unwrap_newtypes

You can add this extension by adding the following attribute at the top of your RON document:

#![enable(unwrap_newtypes)]

This feature enables RON to automatically unwrap simple tuples.

struct NewType(u32);
struct Object {
    pub new_type: NewType,
}

Without unwrap_newtypes, because the value 5 can not be saved into NewType(u32), your RON document would look like this:

(
    new_type: (5),
)

With the unwrap_newtypes extension, this coercion is done automatically. So 5 will be interpreted as (5).

#![enable(unwrap_newtypes)]
(
    new_type: 5,
)

implicit_some

You can add this extension by adding the following attribute at the top of your RON document:

#![enable(implicit_some)]

This feature enables RON to automatically convert any value to Some(value) if the deserialized struct requires it.

struct Object {
    pub value: Option<u32>,
}

Without this feature, you would have to write this RON document.

(
    value: Some(5),
)

Enabling the feature would automatically infer Some(x) if x is given. In this case, RON automatically casts this 5 into a Some(5).

(
    value: 5,
)