Skip to content

Commit bfb8ee7

Browse files
committed
Use Bytes::new instead of &ByteBuf::from
- Optimize usage for byte slice instead of allocating new byte buffer
1 parent 31c33c2 commit bfb8ee7

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/value/index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::Value;
44
use core::ops;
5-
use serde_bytes::ByteBuf;
5+
use serde_bytes::Bytes;
66

77
#[cfg(all(feature = "alloc", not(feature = "std")))]
88
use alloc::string::String;
@@ -35,14 +35,14 @@ impl Index for usize {
3535
impl Index for str {
3636
fn index<'a>(&self, v: &'a Value) -> Option<&'a Value> {
3737
match v {
38-
Value::Dict(ref d) => d.get(&ByteBuf::from(self)),
38+
Value::Dict(ref d) => d.get(Bytes::new(self.as_bytes())),
3939
_ => None,
4040
}
4141
}
4242

4343
fn index_mut<'a>(&self, v: &'a mut Value) -> Option<&'a mut Value> {
4444
match v {
45-
Value::Dict(ref mut d) => d.get_mut(&ByteBuf::from(self)),
45+
Value::Dict(ref mut d) => d.get_mut(Bytes::new(self.as_bytes())),
4646
_ => None,
4747
}
4848
}

tests/de.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
extern crate serde_derive;
33

44
use bt_bencode::{Error, Value};
5-
use serde_bytes::ByteBuf;
65

76
#[derive(Debug, Deserialize, Clone, Eq, Hash, PartialEq)]
87
struct TorrentFile {
@@ -24,13 +23,10 @@ fn test_deserialize_torrent_file_via_value() -> Result<(), Error> {
2423
let torrent_bytes = include_bytes!("ubuntu-18.04.3-live-server-amd64.iso.torrent");
2524
let decoded_value: Value = bt_bencode::from_slice(&torrent_bytes[..])?;
2625

27-
let announce = match decoded_value {
28-
Value::Dict(dict) => match dict.get(&ByteBuf::from(String::from("announce"))) {
29-
Some(Value::ByteStr(s)) => Some(s.clone().into_vec()),
30-
_ => None,
31-
},
32-
_ => None,
33-
};
26+
let announce = decoded_value
27+
.get("announce")
28+
.and_then(|v| v.as_byte_str())
29+
.map(|v| v.to_vec());
3430

3531
assert_eq!(
3632
announce,

0 commit comments

Comments
 (0)