-
Notifications
You must be signed in to change notification settings - Fork 140
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
Add into_
counterparts for all as_
methods
#28
Comments
I plan to submit a pull request for this ticket in the next few days. |
Here's what I currently have: Macro and Methodsmacro_rules! define_as (
($name:ident, $t:ident, $yt:ident) => (
pub fn $name(&self) -> Option<$t> {
match *self {
Yaml::$yt(v) => Some(v),
_ => None
}
}
);
);
macro_rules! define_as_ref (
($name:ident, $t:ty, $yt:ident) => (
pub fn $name(&self) -> Option<$t> {
match *self {
Yaml::$yt(ref v) => Some(v),
_ => None
}
}
);
);
macro_rules! define_into (
($name:ident, $t:ty, $yt:ident) => (
pub fn $name(self) -> Option<$t> {
match self {
Yaml::$yt(v) => Some(v),
_ => None
}
}
);
);
impl Yaml {
define_as!(as_bool, bool, Boolean);
define_as!(as_i64, i64, Integer);
define_as_ref!(as_str, &str, String);
define_as_ref!(as_hash, &Hash, Hash);
define_as_ref!(as_vec, &Array, Array);
define_into!(into_bool, bool, Boolean);
define_into!(into_i64, i64, Integer);
define_into!(into_string, String, String);
define_into!(into_hash, Hash, Hash);
define_into!(into_vec, Array, Array);
pub fn is_null(&self) -> bool {
match *self {
Yaml::Null => true,
_ => false
}
}
pub fn is_badvalue(&self) -> bool {
match *self {
Yaml::BadValue => true,
_ => false
}
}
pub fn as_f64(&self) -> Option<f64> {
match *self {
Yaml::Real(ref v) => {
v.parse::<f64>().ok()
},
_ => None
}
}
pub fn into_f64(self) -> Option<f64> {
match self {
Yaml::Real(v) => {
v.parse::<f64>().ok()
},
_ => None
}
}
} Naïve test caseI'd like to avoid
|
I've implemented |
Background
Conversion method naming conventions
RFC 7087 and The Rust Style Guidelines define the following naming conventions for conversion methods:
as_
to_
into_
Proposed Enhancement
Currently
yaml-rust
providesas_
methods (as_str
,as_hash
,as_vec
, etc.) on theimpl Yaml
. These methods obey the listed convention and do not consume theYaml
object. Addinginto_
methods that consume theYaml
object would allow some callers to reduce their overall memory footprint.Use Case
Code Review Answer (last paragraph)
The text was updated successfully, but these errors were encountered: