pub enum BencodeElem {
    String(String),
    Bytes(Vec<u8>),
    Integer(i64),
    List(Vec<BencodeElem>),
    Dictionary(HashMap<String, BencodeElem>),
    RawDictionary(HashMap<Vec<u8>, BencodeElem>),
}
Expand description

Represent a single bencode element.

There are 4 variants in the spec, but this enum has 6 variants. The extra variants are Bytes (a sequence of bytes that does not represent a valid utf8 string, e.g. a SHA1 block hash), which is considered to be the same as String in the spec, and RawDictionary, which has keys that are not valid utf8 strings. They are best treated differently in actual implementations to make things easier.

Note that the Integer variant here uses i64 explicitly instead of using a type alias like Integer. The reasoning behind this is that if you have to handle bencode directly then what you are doing is relatively low-level. In this case, exposing the underlying type might actually be better.

Variants

String(String)

Bytes(Vec<u8>)

Integer(i64)

List(Vec<BencodeElem>)

Dictionary(HashMap<String, BencodeElem>)

RawDictionary(HashMap<Vec<u8>, BencodeElem>)

Implementations

Parse bytes and return all BencodeElem found.

If bytes is empty, then Ok(vec) will be returned, but vec would be empty as well.

If bytes contains any malformed bencode, or if any other error is encountered (e.g. IOError), then Err(error) will be returned.

Parse the content of the file at path and return all BencodeElem found.

If the file at path is empty, then Ok(vec) will be returned, but vec would be empty as well.

If the file at path contains any malformed bencode, or if any other error is encountered (e.g. IOError), then Err(error) will be returned.

Encode self and write the result to dst.

Encode self and write the result to path.

path must be the path to a file.

“This function will create a file if it does not exist, and will truncate it if it does.”

Note: it is the client’s responsibility to ensure that all directories in path actually exist (e.g. by calling create_dir_all).

Encode self and return the result in a Vec.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

The error type produced by a failed conversion.

Convert the given value into an approximately equivalent representation.

The error type produced by a failed conversion.

Convert the subject into an approximately equivalent representation.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Approximate the subject with the default scheme.

Approximate the subject with a specific scheme.

Approximate the subject to a given type with the default scheme.

Approximate the subject to a given type with a specific scheme.

Convert the subject to a given type.

Attempt to convert the subject to a given type.

Attempt a value conversion of the subject to a given type.

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

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

Converts the given value to a String. Read more

The error type produced by a failed conversion.

Convert the given value into the subject type.

The type returned in the event of a conversion error.

Performs the conversion.

The error type produced by a failed conversion.

Convert the subject into the destination type.

The type returned in the event of a conversion error.

Performs the conversion.

The error type produced by a failed conversion.

Convert the given value into an exactly equivalent representation.

The error type produced by a failed conversion.

Convert the subject into an exactly equivalent representation.