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§

source§

impl BencodeElem

source

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

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.

source

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

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.

source§

impl BencodeElem

source

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

Encode self and write the result to dst.

source

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

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

source

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

Encode self and return the result in a Vec.

Trait Implementations§

source§

impl Clone for BencodeElem

source§

fn clone(&self) -> BencodeElem

Returns a copy 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 BencodeElem

source§

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

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

impl Display for BencodeElem

source§

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

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

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

source§

fn from(val: &'a [u8]) -> BencodeElem

Converts to this type from the input type.
source§

impl<'a> From<&'a str> for BencodeElem

source§

fn from(val: &'a str) -> BencodeElem

Converts to this type from the input type.
source§

impl From<String> for BencodeElem

source§

fn from(val: String) -> BencodeElem

Converts to this type from the input type.
source§

impl From<Vec<u8, Global>> for BencodeElem

source§

fn from(val: Vec<u8>) -> BencodeElem

Converts to this type from the input type.
source§

impl From<i16> for BencodeElem

source§

fn from(val: i16) -> BencodeElem

Converts to this type from the input type.
source§

impl From<i32> for BencodeElem

source§

fn from(val: i32) -> BencodeElem

Converts to this type from the input type.
source§

impl From<i64> for BencodeElem

source§

fn from(val: i64) -> BencodeElem

Converts to this type from the input type.
source§

impl From<i8> for BencodeElem

source§

fn from(val: i8) -> BencodeElem

Converts to this type from the input type.
source§

impl From<u16> for BencodeElem

source§

fn from(val: u16) -> BencodeElem

Converts to this type from the input type.
source§

impl From<u32> for BencodeElem

source§

fn from(val: u32) -> BencodeElem

Converts to this type from the input type.
source§

impl From<u8> for BencodeElem

source§

fn from(val: u8) -> BencodeElem

Converts to this type from the input type.
source§

impl PartialEq<BencodeElem> for BencodeElem

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for BencodeElem

source§

impl StructuralEq for BencodeElem

source§

impl StructuralPartialEq for BencodeElem

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere Scheme: ApproxScheme,

§

type Err = NoError

The error type produced by a failed conversion.
source§

fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>

Convert the given value into an approximately equivalent representation.
source§

impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere Dst: ApproxFrom<Src, Scheme>, Scheme: ApproxScheme,

§

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

The error type produced by a failed conversion.
source§

fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>

Convert the subject into an approximately equivalent representation.
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T, Dst> ConvAsUtil<Dst> for T

source§

fn approx(self) -> Result<Dst, Self::Err>where Self: Sized + ApproxInto<Dst, DefaultApprox>,

Approximate the subject with the default scheme.
source§

fn approx_by<Scheme>(self) -> Result<Dst, Self::Err>where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

Approximate the subject with a specific scheme.
source§

impl<T> ConvUtil for T

source§

fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where Self: Sized + ApproxInto<Dst, DefaultApprox>,

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

fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

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

fn into_as<Dst>(self) -> Dstwhere Self: Sized + Into<Dst>,

Convert the subject to a given type.
source§

fn try_as<Dst>(self) -> Result<Dst, Self::Err>where Self: Sized + TryInto<Dst>,

Attempt to convert the subject to a given type.
source§

fn value_as<Dst>(self) -> Result<Dst, Self::Err>where Self: Sized + ValueInto<Dst>,

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

impl<Src> TryFrom<Src> for Src

§

type Err = NoError

The error type produced by a failed conversion.
source§

fn try_from(src: Src) -> Result<Src, <Src as TryFrom<Src>>::Err>

Convert the given value into the subject type.
source§

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

§

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<Src, Dst> TryInto<Dst> for Srcwhere Dst: TryFrom<Src>,

§

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

The error type produced by a failed conversion.
source§

fn try_into(self) -> Result<Dst, <Src as TryInto<Dst>>::Err>

Convert the subject into the destination type.
source§

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

§

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.
source§

impl<Src> ValueFrom<Src> for Src

§

type Err = NoError

The error type produced by a failed conversion.
source§

fn value_from(src: Src) -> Result<Src, <Src as ValueFrom<Src>>::Err>

Convert the given value into an exactly equivalent representation.
source§

impl<Src, Dst> ValueInto<Dst> for Srcwhere Dst: ValueFrom<Src>,

§

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

The error type produced by a failed conversion.
source§

fn value_into(self) -> Result<Dst, <Src as ValueInto<Dst>>::Err>

Convert the subject into an exactly equivalent representation.