[][src]Enum lava_torrent::bencode::BencodeElem

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

Represent a single bencode element.

There are 4 variants in the spec, but this enum has 5 variants. The extra variant is 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. But 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)
Dictionary(HashMap<String, BencodeElem>)

Methods

impl BencodeElem[src]

pub fn from_bytes<B>(bytes: B) -> Result<Vec<BencodeElem>> where
    B: AsRef<[u8]>, 
[src]

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.

pub fn from_file<P>(path: P) -> Result<Vec<BencodeElem>> where
    P: AsRef<Path>, 
[src]

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.

impl BencodeElem[src]

pub fn write_into<W>(&self, dst: &mut W) -> Result<()> where
    W: Write
[src]

Encode self and write the result to dst.

pub fn write_into_file<P>(&self, path: P) -> Result<()> where
    P: AsRef<Path>, 
[src]

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).

pub fn encode(&self) -> Vec<u8>[src]

Encode self and return the result in a Vec.

Trait Implementations

impl Eq for BencodeElem[src]

impl Clone for BencodeElem[src]

impl PartialEq<BencodeElem> for BencodeElem[src]

impl From<u8> for BencodeElem[src]

impl From<u16> for BencodeElem[src]

impl From<u32> for BencodeElem[src]

impl From<i8> for BencodeElem[src]

impl From<i16> for BencodeElem[src]

impl From<i32> for BencodeElem[src]

impl From<i64> for BencodeElem[src]

impl<'a> From<&'a str> for BencodeElem[src]

impl From<String> for BencodeElem[src]

impl<'a> From<&'a [u8]> for BencodeElem[src]

impl From<Vec<u8>> for BencodeElem[src]

impl Debug for BencodeElem[src]

impl Display for BencodeElem[src]

Auto Trait Implementations

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, Dst> ConvAsUtil<Dst> for T[src]

impl<Src, Scheme> ApproxFrom<Src, Scheme> for Src where
    Scheme: ApproxScheme
[src]

type Err = NoError

The error type produced by a failed conversion.

impl<T> ConvUtil for T[src]

impl<Src> ValueFrom<Src> for Src[src]

type Err = NoError

The error type produced by a failed conversion.

impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src where
    Dst: ApproxFrom<Src, Scheme>,
    Scheme: ApproxScheme
[src]

type Err = <Dst as ApproxFrom<Src, Scheme>>::Err

The error type produced by a failed conversion.

impl<Src, Dst> ValueInto<Dst> for Src where
    Dst: ValueFrom<Src>, 
[src]

type Err = <Dst as ValueFrom<Src>>::Err

The error type produced by a failed conversion.

impl<Src> TryFrom<Src> for Src[src]

type Err = NoError

The error type produced by a failed conversion.

impl<Src, Dst> TryInto<Dst> for Src where
    Dst: TryFrom<Src>, 
[src]

type Err = <Dst as TryFrom<Src>>::Err

The error type produced by a failed conversion.