Skip to content
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

Consider allowing borrowed data in the provided data types #20

Open
Marwes opened this issue Jul 4, 2017 · 0 comments
Open

Consider allowing borrowed data in the provided data types #20

Marwes opened this issue Jul 4, 2017 · 0 comments

Comments

@Marwes
Copy link
Member

Marwes commented Jul 4, 2017

Since all the types in this crate only hold String this library copies more data than it needs it some situations (both serializing and deserializing). Probably the RPC overhead will make these copies insignificant in most cases, but this might be a micro optimization worth doing (measure first of course).

Two ways of doing this.

Replace String fields with Cow<str>. Easy to do but slightly more difficult to use when one do not care about copies.

Parameterize structs containing strings by the string type used.

pub struct Command {
    /// Title of the command, like `save`.
    pub title: String,
    /// The identifier of the actual command handler.
    pub command: String,
    /// Arguments that the command handler should be
    /// invoked with.
    #[serde(skip_serializing_if="Option::is_none")]
    pub arguments: Option<Vec<Value>>,
}
pub struct Command <S = String> {
    /// Title of the command, like `save`.
    pub title: S,
    /// The identifier of the actual command handler.
    pub command: S,
    /// Arguments that the command handler should be
    /// invoked with.
    #[serde(skip_serializing_if="Option::is_none")]
    pub arguments: Option<Vec<Value>>,
}

Setting String as the default value may make this less of a breaking change but I am not sure how much it helps. This would make it possible to use Command<&str> or Command<Cow<str>>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant