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