Skip to content

Commit 06fabbb

Browse files
committed
0.8.7 the take method
1 parent dd1fae5 commit 06fabbb

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
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 <maciej.hirsz@gmail.com>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

src/iterators.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 36 additions & 1 deletion
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,33 @@ 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+
let mut placeholder = JsonValue::Null;
199+
200+
mem::swap(self, &mut placeholder);
201+
202+
placeholder
203+
}
204+
178205
/// Works on `JsonValue::Array` - pushes a new value to the array.
179206
#[must_use]
180207
pub fn push<T>(&mut self, value: T) -> JsonResult<()>
@@ -290,6 +317,8 @@ impl JsonValue {
290317

291318
/// Implements indexing by `usize` to easily access array members:
292319
///
320+
/// ## Example
321+
///
293322
/// ```
294323
/// # use json::JsonValue;
295324
/// let mut array = JsonValue::new_array();
@@ -311,6 +340,8 @@ impl Index<usize> for JsonValue {
311340

312341
/// Implements mutable indexing by `usize` to easily modify array members:
313342
///
343+
/// ## Example
344+
///
314345
/// ```
315346
/// # #[macro_use]
316347
/// # extern crate json;
@@ -347,6 +378,8 @@ impl IndexMut<usize> for JsonValue {
347378

348379
/// Implements indexing by `&str` to easily access object members:
349380
///
381+
/// ## Example
382+
///
350383
/// ```
351384
/// # #[macro_use]
352385
/// # extern crate json;
@@ -391,6 +424,8 @@ impl<'a> Index<&'a String> for JsonValue {
391424

392425
/// Implements mutable indexing by `&str` to easily modify object members:
393426
///
427+
/// ## Example
428+
///
394429
/// ```
395430
/// # #[macro_use]
396431
/// # extern crate json;

0 commit comments

Comments
 (0)