ebarnard / rust-plist

A rusty plist parser.
MIT License
71 stars 42 forks source link

Implement methods that consume Plist and produce Some(value). #10

Closed zummenix closed 8 years ago

zummenix commented 8 years ago

For example now I have:

let dict = try_opt!(plist.as_dictionary());
let sn = dict.get("SerialNumber").and_then(Plist::as_string).map(|s| s.to_owned());

This is somewhat unfortunate with additional allocation by to_owned call. I would like to have something like this:

let mut dict = try_opt!(plist.as_dictionary_mut());
let sn = dict.remove("SerialNumber").and_then(Plist::into_string));

where into_string has signature:

pub fn into_string(self) -> Option<String>;

Maybe there is a better idea.

ebarnard commented 8 years ago

There's always:

let sn = if let Plist::String(s) = dict.remove("SerialNumber") { Some(s) } else { None };

But I agree; into_string and into_data seem like reasonable additions.

zummenix commented 8 years ago

@ebarnard, what do you think about into_array and into_dictionary methods additional to into_string and into_data? I see that those are also can be helpful.

ebarnard commented 8 years ago

Unless you've got a use case I'd rather those them out for now.

You can already get mutable access to storage and you can't reuse the allocated memory for anything, at least not without unsafe.

I'm happy to add them if a concrete need arises.

zummenix commented 8 years ago

Actually my use case with into_dictionary and into_array is the same. But I think we will stick with into_string and into_data for now.