Skip to content

Commit

Permalink
Add inlines and remove default as_u64 impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Oct 24, 2019
1 parent 2cb5e9f commit 58957f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ pub trait ValueTrait:
}

/// Tries to represent the value as an u64
fn as_u64(&self) -> Option<u64> {
self.as_i64().and_then(|u| u.try_into().ok())
}
fn as_u64(&self) -> Option<u64>;

/// returns true if the current value can be represented as a u64
fn is_u64(&self) -> bool {
self.as_u64().is_some()
Expand Down
13 changes: 12 additions & 1 deletion src/value/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn to_value<'v>(s: &'v mut [u8]) -> Result<Value<'v>> {
}

/// Borrowed JSON-DOM Value, consider using the `ValueTrait`
/// to access it'scontent
/// to access it's content
#[derive(Debug, Clone)]
pub enum Value<'v> {
/// null
Expand Down Expand Up @@ -105,20 +105,23 @@ impl<'v> ValueTrait for Value<'v> {
}
}

#[inline]
fn is_null(&self) -> bool {
match self {
Self::Null => true,
_ => false,
}
}

#[inline]
fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
}

#[inline]
fn as_i64(&self) -> Option<i64> {
match self {
Self::I64(i) => Some(*i),
Expand All @@ -127,6 +130,7 @@ impl<'v> ValueTrait for Value<'v> {
}
}

#[inline]
fn as_u64(&self) -> Option<u64> {
#[allow(clippy::cast_sign_loss)]
match self {
Expand All @@ -136,13 +140,15 @@ impl<'v> ValueTrait for Value<'v> {
}
}

#[inline]
fn as_f64(&self) -> Option<f64> {
match self {
Self::F64(i) => Some(*i),
_ => None,
}
}

#[inline]
fn cast_f64(&self) -> Option<f64> {
#[allow(clippy::cast_precision_loss)]
match self {
Expand All @@ -153,6 +159,7 @@ impl<'v> ValueTrait for Value<'v> {
}
}

#[inline]
fn as_str(&self) -> Option<&str> {
use std::borrow::Borrow;
match self {
Expand All @@ -161,27 +168,31 @@ impl<'v> ValueTrait for Value<'v> {
}
}

#[inline]
fn as_array(&self) -> Option<&Vec<Value<'v>>> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
}

#[inline]
fn as_array_mut(&mut self) -> Option<&mut Vec<Value<'v>>> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
}

#[inline]
fn as_object(&self) -> Option<&HashMap<Self::Key, Self>> {
match self {
Self::Object(m) => Some(m),
_ => None,
}
}

#[inline]
fn as_object_mut(&mut self) -> Option<&mut HashMap<Self::Key, Self>> {
match self {
Self::Object(m) => Some(m),
Expand Down
12 changes: 12 additions & 0 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ impl ValueTrait for Value {
}
}

#[inline]
fn is_null(&self) -> bool {
match self {
Self::Null => true,
_ => false,
}
}

#[inline]
fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
}

#[inline]
fn as_i64(&self) -> Option<i64> {
match self {
Self::I64(i) => Some(*i),
Expand All @@ -86,6 +89,7 @@ impl ValueTrait for Value {
}
}

#[inline]
fn as_u64(&self) -> Option<u64> {
#[allow(clippy::cast_sign_loss)]
match self {
Expand All @@ -95,13 +99,15 @@ impl ValueTrait for Value {
}
}

#[inline]
fn as_f64(&self) -> Option<f64> {
match self {
Self::F64(i) => Some(*i),
_ => None,
}
}

#[inline]
fn cast_f64(&self) -> Option<f64> {
#[allow(clippy::cast_precision_loss)]
match self {
Expand All @@ -112,33 +118,39 @@ impl ValueTrait for Value {
}
}

#[inline]
fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s.as_str()),
_ => None,
}
}

#[inline]
fn as_array(&self) -> Option<&Vec<Self>> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
}

#[inline]
fn as_array_mut(&mut self) -> Option<&mut Vec<Self>> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
}

#[inline]
fn as_object(&self) -> Option<&HashMap<Self::Key, Self>> {
match self {
Self::Object(m) => Some(m),
_ => None,
}
}

#[inline]
fn as_object_mut(&mut self) -> Option<&mut HashMap<Self::Key, Self>> {
match self {
Self::Object(m) => Some(m),
Expand Down

0 comments on commit 58957f8

Please sign in to comment.