pub struct TorrentBuilder { /* private fields */ }
Expand description

Builder for creating Torrents from files.

This struct is used for creating Torrents, so that you can encode/serialize them to .torrent files. If you want to read existing .torrent files then use Torrent::read_from_file() or Torrent::read_from_bytes().

Required fields: path and piece_length. They are set when calling the constructor new().

Optional fields can be set by calling the corresponding methods (e.g. set_announce()). Fields can be updated in the same way.

Notes

Hidden Files

*nix hidden files/dirs are ignored.

Reasoning: when handling these special “files”, there are many decisions to make:

  • Should they be ignored, included, or selectively ignored/included?
  • Should included/ignored entries be marked specially (e.g. BEP 47)?
  • Should users be allowed to configure the settings?
  • If users can configure the settings, what would be the ideal defaults?

Apparently it’s not easy to make these decisions. Therefore these files are ignored for now. Clients like Deluge and qBittorrent also ignore hidden entries.

Parallel Hashing

By default, pieces are hashed in parallel. The default level of parallelism is equal to the number of physical cores. To adjust the parallelism level or to force single-threaded hashing, use set_num_threads(). Note that this setting is specific to each builder and not global.

Implementations§

source§

impl TorrentBuilder

source

pub fn new<P>(path: P, piece_length: Integer) -> TorrentBuilderwhere P: AsRef<Path>,

Create a new TorrentBuilder with required fields set.

The caller has to ensure that the inputs are valid, as this method does not validate its inputs. If they turn out to be invalid, calling build() later will fail.

NOTE: A valid piece_length is larger than 0 AND is a power of 2.

source

pub fn build(self) -> Result<Torrent, LavaTorrentError>

Build a Torrent from this TorrentBuilder.

If name is not set, then the last component of path will be used as the Torrent’s name field.

build() does not provide comprehensive validation of any input. Basic cases such as setting announce to an empty string will be detected and Err will be returned. But more complicated cases such as using an invalid url as announce won’t be detected. Again, the caller has to ensure that the values given to a TorrentBuilder are valid.

source

pub fn build_non_blocking(self) -> Result<TorrentBuild, LavaTorrentError>

Like build(), but non-blocking.

Example
use lava_torrent::torrent::v1::TorrentBuilder;

let build = TorrentBuilder::new("dir/", 1048576).build_non_blocking().unwrap();

while !build.is_finished() {
    println!("torrent build progress: {}%", build.get_progress());
    std::thread::sleep(std::time::Duration::from_millis(100));
}

let torrent = build.get_output().unwrap();
torrent.write_into_file("sample.torrent").unwrap();
source

pub fn set_announce(self, announce: Option<String>) -> TorrentBuilder

Set the announce field of the Torrent to be built.

Calling this method multiple times will simply override previous settings.

The caller has to ensure that announce is valid, as this method does not validate its value. If announce turns out to be invalid, calling build() later will fail.

source

pub fn set_announce_list(self, announce_list: AnnounceList) -> TorrentBuilder

Set the announce_list field of the Torrent to be built.

Calling this method multiple times will simply override previous settings.

The caller has to ensure that announce_list is valid, as this method does not validate its value. If announce_list turns out to be invalid, calling build() later will fail.

source

pub fn set_name(self, name: String) -> TorrentBuilder

Set the name field of the Torrent to be built.

Calling this method multiple times will simply override previous settings.

The caller has to ensure that name is valid, as this method does not validate its value. If name turns out to be invalid, calling build() later will fail.

source

pub fn set_path<P>(self, path: P) -> TorrentBuilderwhere P: AsRef<Path>,

Set the path to the file(s) from which the Torrent will be built.

Calling this method multiple times will simply override previous settings.

The caller has to ensure that path is valid, as this method does not validate its value. If path turns out to be invalid, calling build() later will fail.

source

pub fn set_piece_length(self, piece_length: Integer) -> TorrentBuilder

Set the piece_length field of the Torrent to be built.

Calling this method multiple times will simply override previous settings.

The caller has to ensure that piece_length is valid, as this method does not validate its value. If piece_length turns out to be invalid, calling build() later will fail.

NOTE: A valid piece_length is larger than 0 AND is a power of 2.

source

pub fn add_extra_field(self, key: String, val: BencodeElem) -> TorrentBuilder

Add an extra field to Torrent (i.e. to the root dictionary).

Calling this method multiple times with the same key will simply override previous settings.

The caller has to ensure that key and val are valid, as this method does not validate their values. If they turn out to be invalid, calling build() later will fail.

source

pub fn add_extra_info_field( self, key: String, val: BencodeElem ) -> TorrentBuilder

Add an extra info field to Torrent (i.e. to the info dictionary).

Calling this method multiple times with the same key will simply override previous settings.

The caller has to ensure that key and val are valid, as this method does not validate their values. If they turn out to be invalid, calling build() later will fail.

source

pub fn set_privacy(self, is_private: bool) -> TorrentBuilder

Make the Torrent private or public, as defined in BEP 27.

Calling this method multiple times will simply override previous settings.

source

pub fn set_num_threads(self, num_threads: usize) -> TorrentBuilder

Change the number of threads used when hashing pieces.

If set to 0, the number of threads used will be equal to the number of physical cores. This is also the default behavior.

Set this to 1 if you prefer single-threaded hashing.

Trait Implementations§

source§

impl Clone for TorrentBuilder

source§

fn clone(&self) -> TorrentBuilder

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 TorrentBuilder

source§

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

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

impl Default for TorrentBuilder

source§

fn default() -> TorrentBuilder

Returns the “default value” for a type. Read more
source§

impl PartialEq<TorrentBuilder> for TorrentBuilder

source§

fn eq(&self, other: &TorrentBuilder) -> 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 TorrentBuilder

source§

impl StructuralEq for TorrentBuilder

source§

impl StructuralPartialEq for TorrentBuilder

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