Enum lava_torrent::bencode::BencodeElem
source · [−]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
sourceimpl BencodeElem
impl BencodeElem
sourcepub fn from_bytes<B>(bytes: B) -> Result<Vec<BencodeElem>, LavaTorrentError> where
B: AsRef<[u8]>,
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.
sourcepub fn from_file<P>(path: P) -> Result<Vec<BencodeElem>, LavaTorrentError> where
P: AsRef<Path>,
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.
sourceimpl BencodeElem
impl BencodeElem
sourcepub fn write_into<W>(&self, dst: &mut W) -> Result<(), LavaTorrentError> where
W: Write,
pub fn write_into<W>(&self, dst: &mut W) -> Result<(), LavaTorrentError> where
W: Write,
Encode self
and write the result to dst
.
sourcepub fn write_into_file<P>(&self, path: P) -> Result<(), LavaTorrentError> where
P: AsRef<Path>,
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
).
Trait Implementations
sourceimpl Clone for BencodeElem
impl Clone for BencodeElem
sourcefn clone(&self) -> BencodeElem
fn clone(&self) -> BencodeElem
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for BencodeElem
impl Debug for BencodeElem
sourceimpl Display for BencodeElem
impl Display for BencodeElem
sourceimpl<'a> From<&'a str> for BencodeElem
impl<'a> From<&'a str> for BencodeElem
sourcefn from(val: &'a str) -> BencodeElem
fn from(val: &'a str) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<String> for BencodeElem
impl From<String> for BencodeElem
sourcefn from(val: String) -> BencodeElem
fn from(val: String) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<Vec<u8, Global>> for BencodeElem
impl From<Vec<u8, Global>> for BencodeElem
sourcefn from(val: Vec<u8>) -> BencodeElem
fn from(val: Vec<u8>) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<i16> for BencodeElem
impl From<i16> for BencodeElem
sourcefn from(val: i16) -> BencodeElem
fn from(val: i16) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<i32> for BencodeElem
impl From<i32> for BencodeElem
sourcefn from(val: i32) -> BencodeElem
fn from(val: i32) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<i64> for BencodeElem
impl From<i64> for BencodeElem
sourcefn from(val: i64) -> BencodeElem
fn from(val: i64) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<i8> for BencodeElem
impl From<i8> for BencodeElem
sourcefn from(val: i8) -> BencodeElem
fn from(val: i8) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<u16> for BencodeElem
impl From<u16> for BencodeElem
sourcefn from(val: u16) -> BencodeElem
fn from(val: u16) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<u32> for BencodeElem
impl From<u32> for BencodeElem
sourcefn from(val: u32) -> BencodeElem
fn from(val: u32) -> BencodeElem
Converts to this type from the input type.
sourceimpl From<u8> for BencodeElem
impl From<u8> for BencodeElem
sourcefn from(val: u8) -> BencodeElem
fn from(val: u8) -> BencodeElem
Converts to this type from the input type.
sourceimpl PartialEq<BencodeElem> for BencodeElem
impl PartialEq<BencodeElem> for BencodeElem
sourcefn eq(&self, other: &BencodeElem) -> bool
fn eq(&self, other: &BencodeElem) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &BencodeElem) -> bool
fn ne(&self, other: &BencodeElem) -> bool
This method tests for !=
.
impl Eq for BencodeElem
impl StructuralEq for BencodeElem
impl StructuralPartialEq for BencodeElem
Auto Trait Implementations
impl RefUnwindSafe for BencodeElem
impl Send for BencodeElem
impl Sync for BencodeElem
impl Unpin for BencodeElem
impl UnwindSafe for BencodeElem
Blanket Implementations
sourceimpl<Src, Scheme> ApproxFrom<Src, Scheme> for Src where
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Src where
Scheme: ApproxScheme,
sourcefn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Convert the given value into an approximately equivalent representation.
sourceimpl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src where
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src where
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
The error type produced by a failed conversion.
sourcefn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Convert the subject into an approximately equivalent representation.
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
sourcefn approx(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
fn approx(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
Approximate the subject with the default scheme.
sourcefn approx_by<Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
fn approx_by<Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
Approximate the subject with a specific scheme.
sourceimpl<T> ConvUtil for T
impl<T> ConvUtil for T
sourcefn approx_as<Dst>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
Approximate the subject to a given type with the default scheme.
sourcefn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
Approximate the subject to a given type with a specific scheme.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more