Enum BNode

Source
pub enum BNode {
    Integer(i64),
    Bytes(Vec<u8>),
    List(BList),
    Dict(BDict),
}

Variants§

§

Integer(i64)

§

Bytes(Vec<u8>)

§

List(BList)

§

Dict(BDict)

Implementations§

Source§

impl BNode

Source

pub fn serialize<W>(&self, buf: &mut W) -> Result<usize>
where W: Write,

Examples found in repository?
examples/ser/main.rs (line 11)
3fn main() {
4    let mut dict = BDict::new();
5    dict.insert("bar".to_string(), "spam".into());
6    dict.insert("foo".to_string(), 42.into());
7
8    let bnode: BNode = dict.into();
9
10    let mut buf = vec![];
11    bnode.serialize(&mut buf).unwrap();
12    assert_eq!("d3:bar4:spam3:fooi42ee".as_bytes(), buf)
13}
More examples
Hide additional examples
examples/bufwriter/main.rs (line 19)
4fn main() {
5    let mut dict = BDict::new();
6    dict.insert("bar".to_string(), "spam".into());
7    dict.insert("foo".to_string(), 42.into());
8
9    let bnode: BNode = dict.into();
10
11    let mut file = File::create(
12        env::current_dir()
13            .unwrap()
14            .join("examples")
15            .join("bufwriter")
16            .join("bufwriter-test.torrent"),
17    )
18    .unwrap();
19    bnode.serialize(&mut file).unwrap();
20}
Source

pub fn as_integer(&self) -> Result<&i64, String>

Examples found in repository?
examples/de/main.rs (line 6)
1fn main() {
2    let b = "d3:inti233e3:lstl7:bencodeee";
3    let node = bencodex::parse(&mut b.bytes()).unwrap();
4
5    let dict = node.as_dict().unwrap();
6    let int = dict.get("int").unwrap().as_integer().unwrap();
7
8    assert_eq!(int, &233);
9    let list = dict.get("lst").unwrap().as_list().unwrap();
10    assert_eq!(list.len(), 1);
11    assert_eq!(
12        list.get(0).unwrap().as_bytes().unwrap(),
13        "bencode".as_bytes()
14    );
15}
More examples
Hide additional examples
examples/bufreader/main.rs (line 41)
21fn main() {
22    let f = File::open(
23        env::current_dir()
24            .unwrap()
25            .join("examples")
26            .join("bufreader")
27            .join("bufreader-test.torrent"),
28    )
29    .unwrap();
30    let reader = BufReader::new(f);
31
32    let mut adapter = Adapter {
33        bytes: reader.bytes(),
34    };
35    let bnode = bencodex::parse(&mut adapter).unwrap();
36    let dict = bnode.as_dict().unwrap();
37    assert_eq!(
38        dict.get("bar").unwrap().as_bytes().unwrap(),
39        "spam".as_bytes()
40    );
41    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
42}
Source

pub fn as_bytes(&self) -> Result<&[u8], String>

Examples found in repository?
examples/de/main.rs (line 12)
1fn main() {
2    let b = "d3:inti233e3:lstl7:bencodeee";
3    let node = bencodex::parse(&mut b.bytes()).unwrap();
4
5    let dict = node.as_dict().unwrap();
6    let int = dict.get("int").unwrap().as_integer().unwrap();
7
8    assert_eq!(int, &233);
9    let list = dict.get("lst").unwrap().as_list().unwrap();
10    assert_eq!(list.len(), 1);
11    assert_eq!(
12        list.get(0).unwrap().as_bytes().unwrap(),
13        "bencode".as_bytes()
14    );
15}
More examples
Hide additional examples
examples/bufreader/main.rs (line 38)
21fn main() {
22    let f = File::open(
23        env::current_dir()
24            .unwrap()
25            .join("examples")
26            .join("bufreader")
27            .join("bufreader-test.torrent"),
28    )
29    .unwrap();
30    let reader = BufReader::new(f);
31
32    let mut adapter = Adapter {
33        bytes: reader.bytes(),
34    };
35    let bnode = bencodex::parse(&mut adapter).unwrap();
36    let dict = bnode.as_dict().unwrap();
37    assert_eq!(
38        dict.get("bar").unwrap().as_bytes().unwrap(),
39        "spam".as_bytes()
40    );
41    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
42}
Source

pub fn as_list(&self) -> Result<&[BNode], String>

Examples found in repository?
examples/de/main.rs (line 9)
1fn main() {
2    let b = "d3:inti233e3:lstl7:bencodeee";
3    let node = bencodex::parse(&mut b.bytes()).unwrap();
4
5    let dict = node.as_dict().unwrap();
6    let int = dict.get("int").unwrap().as_integer().unwrap();
7
8    assert_eq!(int, &233);
9    let list = dict.get("lst").unwrap().as_list().unwrap();
10    assert_eq!(list.len(), 1);
11    assert_eq!(
12        list.get(0).unwrap().as_bytes().unwrap(),
13        "bencode".as_bytes()
14    );
15}
Source

pub fn as_dict(&self) -> Result<&BDict, String>

Examples found in repository?
examples/de/main.rs (line 5)
1fn main() {
2    let b = "d3:inti233e3:lstl7:bencodeee";
3    let node = bencodex::parse(&mut b.bytes()).unwrap();
4
5    let dict = node.as_dict().unwrap();
6    let int = dict.get("int").unwrap().as_integer().unwrap();
7
8    assert_eq!(int, &233);
9    let list = dict.get("lst").unwrap().as_list().unwrap();
10    assert_eq!(list.len(), 1);
11    assert_eq!(
12        list.get(0).unwrap().as_bytes().unwrap(),
13        "bencode".as_bytes()
14    );
15}
More examples
Hide additional examples
examples/bufreader/main.rs (line 36)
21fn main() {
22    let f = File::open(
23        env::current_dir()
24            .unwrap()
25            .join("examples")
26            .join("bufreader")
27            .join("bufreader-test.torrent"),
28    )
29    .unwrap();
30    let reader = BufReader::new(f);
31
32    let mut adapter = Adapter {
33        bytes: reader.bytes(),
34    };
35    let bnode = bencodex::parse(&mut adapter).unwrap();
36    let dict = bnode.as_dict().unwrap();
37    assert_eq!(
38        dict.get("bar").unwrap().as_bytes().unwrap(),
39        "spam".as_bytes()
40    );
41    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
42}

Trait Implementations§

Source§

impl Clone for BNode

Source§

fn clone(&self) -> BNode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&[u8]> for BNode

Source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for BNode

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<BTreeMap<String, BNode>> for BNode

Source§

fn from(value: BTreeMap<String, BNode>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for BNode

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<BNode>> for BNode

Source§

fn from(value: Vec<BNode>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for BNode

Source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for BNode

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for BNode

Source§

fn eq(&self, other: &BNode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for BNode

Source§

impl StructuralPartialEq for BNode

Auto Trait Implementations§

§

impl Freeze for BNode

§

impl RefUnwindSafe for BNode

§

impl Send for BNode

§

impl Sync for BNode

§

impl Unpin for BNode

§

impl UnwindSafe for BNode

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.