Skip to content

Commit bbef847

Browse files
authored
Merge pull request #61 from maciejhirsz/0.8.7
0.8.7 the `take` method
2 parents dd1fae5 + f5dfea1 commit bbef847

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json"
3-
version = "0.8.6"
3+
version = "0.8.7"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

src/iterators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::btree_map;
22
use std::slice;
3-
use std::iter::{Iterator, DoubleEndedIterator};
3+
use std::iter::{ Iterator, DoubleEndedIterator };
44
use JsonValue;
55

66
pub enum Members<'a> {

src/value.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
22
use std::ops::{ Index, IndexMut, Deref };
33
use iterators::{ Members, MembersMut, Entries, EntriesMut };
44
use { JsonResult, JsonError };
5-
use std::{ usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32 };
5+
use std::{ mem, usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32 };
66

77
macro_rules! f64_to_unsinged {
88
($unsigned:ident, $value:expr) => {
@@ -175,6 +175,29 @@ impl JsonValue {
175175
}
176176
}
177177

178+
/// Take over the ownership of the value, leaving `Null` in it's place.
179+
///
180+
/// ## Example
181+
///
182+
/// ```
183+
/// # #[macro_use] extern crate json;
184+
/// # fn main() {
185+
/// let mut data = array!["Foo", 42];
186+
///
187+
/// let first = data[0].take();
188+
/// let second = data[1].take();
189+
///
190+
/// assert!(first == "Foo");
191+
/// assert!(second == 42);
192+
///
193+
/// assert!(data[0].is_null());
194+
/// assert!(data[1].is_null());
195+
/// # }
196+
/// ```
197+
pub fn take(&mut self) -> JsonValue {
198+
mem::replace(self, JsonValue::Null)
199+
}
200+
178201
/// Works on `JsonValue::Array` - pushes a new value to the array.
179202
#[must_use]
180203
pub fn push<T>(&mut self, value: T) -> JsonResult<()>
@@ -290,6 +313,8 @@ impl JsonValue {
290313

291314
/// Implements indexing by `usize` to easily access array members:
292315
///
316+
/// ## Example
317+
///
293318
/// ```
294319
/// # use json::JsonValue;
295320
/// let mut array = JsonValue::new_array();
@@ -311,6 +336,8 @@ impl Index<usize> for JsonValue {
311336

312337
/// Implements mutable indexing by `usize` to easily modify array members:
313338
///
339+
/// ## Example
340+
///
314341
/// ```
315342
/// # #[macro_use]
316343
/// # extern crate json;
@@ -347,6 +374,8 @@ impl IndexMut<usize> for JsonValue {
347374

348375
/// Implements indexing by `&str` to easily access object members:
349376
///
377+
/// ## Example
378+
///
350379
/// ```
351380
/// # #[macro_use]
352381
/// # extern crate json;
@@ -391,6 +420,8 @@ impl<'a> Index<&'a String> for JsonValue {
391420

392421
/// Implements mutable indexing by `&str` to easily modify object members:
393422
///
423+
/// ## Example
424+
///
394425
/// ```
395426
/// # #[macro_use]
396427
/// # extern crate json;

0 commit comments

Comments
 (0)