Skip to content

Commit def2ad2

Browse files
committed
Allow indexing by String and &String
1 parent 235c481 commit def2ad2

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/value.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::BTreeMap;
2-
use std::ops::{ Index, IndexMut };
2+
use std::ops::{ Index, IndexMut, Deref };
33
use iterators::{ Members, MembersMut, Entries, EntriesMut };
44
use { JsonResult, JsonError };
55
use std::{ usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32 };
@@ -496,6 +496,22 @@ impl<'a> Index<&'a str> for JsonValue {
496496
}
497497
}
498498

499+
impl Index<String> for JsonValue {
500+
type Output = JsonValue;
501+
502+
fn index(&self, index: String) -> &JsonValue {
503+
self.index(index.deref())
504+
}
505+
}
506+
507+
impl<'a> Index<&'a String> for JsonValue {
508+
type Output = JsonValue;
509+
510+
fn index(&self, index: &String) -> &JsonValue {
511+
self.index(index.deref())
512+
}
513+
}
514+
499515
/// Implements mutable indexing by `&str` to easily modify object members:
500516
///
501517
/// ```
@@ -526,3 +542,15 @@ impl<'a> IndexMut<&'a str> for JsonValue {
526542
}
527543
}
528544
}
545+
546+
impl IndexMut<String> for JsonValue {
547+
fn index_mut(&mut self, index: String) -> &mut JsonValue {
548+
self.index_mut(index.deref())
549+
}
550+
}
551+
552+
impl<'a> IndexMut<&'a String> for JsonValue {
553+
fn index_mut(&mut self, index: &String) -> &mut JsonValue {
554+
self.index_mut(index.deref())
555+
}
556+
}

tests/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,72 @@ fn null_len() {
629629
assert_eq!(data.len(), 0);
630630
}
631631

632+
#[test]
633+
fn index_by_str() {
634+
let data = object!{
635+
"foo" => "bar"
636+
};
637+
638+
assert_eq!(data["foo"], "bar");
639+
}
640+
641+
#[test]
642+
fn index_by_string() {
643+
let data = object!{
644+
"foo" => "bar"
645+
};
646+
647+
assert_eq!(data["foo".to_string()], "bar");
648+
}
649+
650+
#[test]
651+
fn index_by_string_ref() {
652+
let data = object!{
653+
"foo" => "bar"
654+
};
655+
656+
let key = "foo".to_string();
657+
let ref key_ref = key;
658+
659+
assert_eq!(data[key_ref], "bar");
660+
}
661+
662+
#[test]
663+
fn index_mut_by_str() {
664+
let mut data = object!{
665+
"foo" => Null
666+
};
667+
668+
data["foo"] = "bar".into();
669+
670+
assert_eq!(data["foo"], "bar");
671+
}
672+
673+
#[test]
674+
fn index_mut_by_string() {
675+
let mut data = object!{
676+
"foo" => Null
677+
};
678+
679+
data["foo".to_string()] = "bar".into();
680+
681+
assert_eq!(data["foo"], "bar");
682+
}
683+
684+
#[test]
685+
fn index_mut_by_string_ref() {
686+
let mut data = object!{
687+
"foo" => Null
688+
};
689+
690+
let key = "foo".to_string();
691+
let ref key_ref = key;
692+
693+
data[key_ref] = "bar".into();
694+
695+
assert_eq!(data["foo"], "bar");
696+
}
697+
632698
#[test]
633699
fn fmt_string() {
634700
let data: JsonValue = "foobar".into();

0 commit comments

Comments
 (0)