Remove some trait bounds
This commit is contained in:
parent
44c760bcc2
commit
4f07b0f785
3 changed files with 15 additions and 19 deletions
|
|
@ -2,7 +2,6 @@ use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
ops::Deref,
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
|
|
@ -139,7 +138,7 @@ struct TorrentManager {
|
||||||
force_tracker_interval: Option<Duration>,
|
force_tracker_interval: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_lengths<ByteBuf: Clone + Deref<Target = [u8]>>(
|
fn make_lengths<ByteBuf: AsRef<[u8]>>(
|
||||||
torrent: &TorrentMetaV1Info<ByteBuf>,
|
torrent: &TorrentMetaV1Info<ByteBuf>,
|
||||||
) -> anyhow::Result<Lengths> {
|
) -> anyhow::Result<Lengths> {
|
||||||
let total_length = torrent.iter_file_lengths().sum();
|
let total_length = torrent.iter_file_lengths().sum();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{fmt::Write, ops::Deref, path::PathBuf};
|
use std::{fmt::Write, path::PathBuf};
|
||||||
|
|
||||||
use bencode::BencodeDeserializer;
|
use bencode::BencodeDeserializer;
|
||||||
use buffers::{ByteBuf, ByteString};
|
use buffers::{ByteBuf, ByteString};
|
||||||
|
|
@ -8,7 +8,7 @@ use serde::Deserialize;
|
||||||
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
||||||
pub type TorrentMetaV1Owned = TorrentMetaV1<ByteString>;
|
pub type TorrentMetaV1Owned = TorrentMetaV1<ByteString>;
|
||||||
|
|
||||||
pub fn torrent_from_bytes<'de, ByteBuf: Clone + Deserialize<'de>>(
|
pub fn torrent_from_bytes<'de, ByteBuf: Deserialize<'de>>(
|
||||||
buf: &'de [u8],
|
buf: &'de [u8],
|
||||||
) -> anyhow::Result<TorrentMetaV1<ByteBuf>> {
|
) -> anyhow::Result<TorrentMetaV1<ByteBuf>> {
|
||||||
let mut de = BencodeDeserializer::new_from_buf(buf);
|
let mut de = BencodeDeserializer::new_from_buf(buf);
|
||||||
|
|
@ -19,7 +19,7 @@ pub fn torrent_from_bytes<'de, ByteBuf: Clone + Deserialize<'de>>(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct TorrentMetaV1<BufType: Clone> {
|
pub struct TorrentMetaV1<BufType> {
|
||||||
pub announce: BufType,
|
pub announce: BufType,
|
||||||
#[serde(rename = "announce-list")]
|
#[serde(rename = "announce-list")]
|
||||||
pub announce_list: Vec<Vec<BufType>>,
|
pub announce_list: Vec<Vec<BufType>>,
|
||||||
|
|
@ -38,14 +38,14 @@ pub struct TorrentMetaV1<BufType: Clone> {
|
||||||
pub info_hash: [u8; 20],
|
pub info_hash: [u8; 20],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BufType: Clone> TorrentMetaV1<BufType> {
|
impl<BufType> TorrentMetaV1<BufType> {
|
||||||
pub fn iter_announce(&self) -> impl Iterator<Item = &BufType> {
|
pub fn iter_announce(&self) -> impl Iterator<Item = &BufType> {
|
||||||
std::iter::once(&self.announce).chain(self.announce_list.iter().flatten())
|
std::iter::once(&self.announce).chain(self.announce_list.iter().flatten())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct TorrentMetaV1Info<BufType: Clone> {
|
pub struct TorrentMetaV1Info<BufType> {
|
||||||
pub name: Option<BufType>,
|
pub name: Option<BufType>,
|
||||||
pub pieces: BufType,
|
pub pieces: BufType,
|
||||||
#[serde(rename = "piece length")]
|
#[serde(rename = "piece length")]
|
||||||
|
|
@ -116,17 +116,17 @@ impl<'a, ByteBuf> FileIteratorName<'a, ByteBuf> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BufType: Clone + Deref<Target = [u8]>> TorrentMetaV1Info<BufType> {
|
impl<BufType: AsRef<[u8]>> TorrentMetaV1Info<BufType> {
|
||||||
pub fn get_hash(&self, piece: u32) -> Option<&[u8]> {
|
pub fn get_hash(&self, piece: u32) -> Option<&[u8]> {
|
||||||
let start = piece as usize * 20;
|
let start = piece as usize * 20;
|
||||||
let end = start + 20;
|
let end = start + 20;
|
||||||
let expected_hash = self.pieces.deref().get(start..end)?;
|
let expected_hash = self.pieces.as_ref().get(start..end)?;
|
||||||
Some(expected_hash)
|
Some(expected_hash)
|
||||||
}
|
}
|
||||||
pub fn compare_hash(&self, piece: u32, hash: [u8; 20]) -> Option<bool> {
|
pub fn compare_hash(&self, piece: u32, hash: [u8; 20]) -> Option<bool> {
|
||||||
let start = piece as usize * 20;
|
let start = piece as usize * 20;
|
||||||
let end = start + 20;
|
let end = start + 20;
|
||||||
let expected_hash = self.pieces.deref().get(start..end)?;
|
let expected_hash = self.pieces.as_ref().get(start..end)?;
|
||||||
Some(expected_hash == hash)
|
Some(expected_hash == hash)
|
||||||
}
|
}
|
||||||
pub fn iter_filenames_and_lengths(
|
pub fn iter_filenames_and_lengths(
|
||||||
|
|
@ -158,14 +158,14 @@ impl<BufType: Clone + Deref<Target = [u8]>> TorrentMetaV1Info<BufType> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct TorrentMetaV1File<BufType: Clone> {
|
pub struct TorrentMetaV1File<BufType> {
|
||||||
pub length: u64,
|
pub length: u64,
|
||||||
pub path: Vec<BufType>,
|
pub path: Vec<BufType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BufType> TorrentMetaV1File<BufType>
|
impl<BufType> TorrentMetaV1File<BufType>
|
||||||
where
|
where
|
||||||
BufType: Clone + AsRef<[u8]>,
|
BufType: AsRef<[u8]>,
|
||||||
{
|
{
|
||||||
pub fn full_path(&self, parent: &mut PathBuf) -> anyhow::Result<()> {
|
pub fn full_path(&self, parent: &mut PathBuf) -> anyhow::Result<()> {
|
||||||
for p in self.path.iter() {
|
for p in self.path.iter() {
|
||||||
|
|
@ -178,8 +178,7 @@ where
|
||||||
|
|
||||||
impl<ByteBuf> CloneToOwned for TorrentMetaV1File<ByteBuf>
|
impl<ByteBuf> CloneToOwned for TorrentMetaV1File<ByteBuf>
|
||||||
where
|
where
|
||||||
ByteBuf: CloneToOwned + Clone,
|
ByteBuf: CloneToOwned,
|
||||||
<ByteBuf as CloneToOwned>::Target: Clone,
|
|
||||||
{
|
{
|
||||||
type Target = TorrentMetaV1File<<ByteBuf as CloneToOwned>::Target>;
|
type Target = TorrentMetaV1File<<ByteBuf as CloneToOwned>::Target>;
|
||||||
|
|
||||||
|
|
@ -193,8 +192,7 @@ where
|
||||||
|
|
||||||
impl<ByteBuf> CloneToOwned for TorrentMetaV1Info<ByteBuf>
|
impl<ByteBuf> CloneToOwned for TorrentMetaV1Info<ByteBuf>
|
||||||
where
|
where
|
||||||
ByteBuf: CloneToOwned + Clone,
|
ByteBuf: CloneToOwned,
|
||||||
<ByteBuf as CloneToOwned>::Target: Clone,
|
|
||||||
{
|
{
|
||||||
type Target = TorrentMetaV1Info<<ByteBuf as CloneToOwned>::Target>;
|
type Target = TorrentMetaV1Info<<ByteBuf as CloneToOwned>::Target>;
|
||||||
|
|
||||||
|
|
@ -212,8 +210,7 @@ where
|
||||||
|
|
||||||
impl<ByteBuf> CloneToOwned for TorrentMetaV1<ByteBuf>
|
impl<ByteBuf> CloneToOwned for TorrentMetaV1<ByteBuf>
|
||||||
where
|
where
|
||||||
ByteBuf: CloneToOwned + Clone,
|
ByteBuf: CloneToOwned,
|
||||||
<ByteBuf as CloneToOwned>::Target: Clone,
|
|
||||||
{
|
{
|
||||||
type Target = TorrentMetaV1<<ByteBuf as CloneToOwned>::Target>;
|
type Target = TorrentMetaV1<<ByteBuf as CloneToOwned>::Target>;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ struct Opts {
|
||||||
single_thread_runtime: bool,
|
single_thread_runtime: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_only_files<ByteBuf: Clone + std::ops::Deref<Target = [u8]> + AsRef<[u8]>>(
|
fn compute_only_files<ByteBuf: AsRef<[u8]>>(
|
||||||
torrent: &TorrentMetaV1Info<ByteBuf>,
|
torrent: &TorrentMetaV1Info<ByteBuf>,
|
||||||
filename_re: &str,
|
filename_re: &str,
|
||||||
) -> anyhow::Result<Vec<usize>> {
|
) -> anyhow::Result<Vec<usize>> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue