Enum lava_torrent::bencode::BencodeElem
[−]
[src]
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)
List(Vec<BencodeElem>)
Dictionary(HashMap<String, BencodeElem>)
Methods
impl BencodeElem
[src]
pub fn from_bytes<B>(bytes: B) -> Result<Vec<BencodeElem>> where
B: AsRef<[u8]>,
[src]
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.
pub fn from_file<P>(path: P) -> Result<Vec<BencodeElem>> where
P: AsRef<Path>,
[src]
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.
impl BencodeElem
[src]
pub fn write_into<W>(&self, dst: &mut W) -> Result<()> where
W: Write,
[src]
W: Write,
Encode self
and write the result to dst
.
pub fn write_into_file<P>(&self, path: P) -> Result<()> where
P: AsRef<Path>,
[src]
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."
pub fn encode(&self) -> Vec<u8>
[src]
Encode self
and return the result in a Vec
.
Trait Implementations
impl Clone for BencodeElem
[src]
fn clone(&self) -> BencodeElem
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for BencodeElem
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl Eq for BencodeElem
[src]
impl PartialEq for BencodeElem
[src]
fn eq(&self, __arg_0: &BencodeElem) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &BencodeElem) -> bool
[src]
This method tests for !=
.
impl From<u8> for BencodeElem
[src]
fn from(val: u8) -> BencodeElem
[src]
Performs the conversion.
impl From<u16> for BencodeElem
[src]
fn from(val: u16) -> BencodeElem
[src]
Performs the conversion.
impl From<u32> for BencodeElem
[src]
fn from(val: u32) -> BencodeElem
[src]
Performs the conversion.
impl From<i8> for BencodeElem
[src]
fn from(val: i8) -> BencodeElem
[src]
Performs the conversion.
impl From<i16> for BencodeElem
[src]
fn from(val: i16) -> BencodeElem
[src]
Performs the conversion.
impl From<i32> for BencodeElem
[src]
fn from(val: i32) -> BencodeElem
[src]
Performs the conversion.
impl From<i64> for BencodeElem
[src]
fn from(val: i64) -> BencodeElem
[src]
Performs the conversion.
impl<'a> From<&'a str> for BencodeElem
[src]
fn from(val: &'a str) -> BencodeElem
[src]
Performs the conversion.
impl From<String> for BencodeElem
[src]
fn from(val: String) -> BencodeElem
[src]
Performs the conversion.
impl<'a> From<&'a [u8]> for BencodeElem
[src]
impl From<Vec<u8>> for BencodeElem
[src]
fn from(val: Vec<u8>) -> BencodeElem
[src]
Performs the conversion.