Use bytes crate for zerocopy and memory re-use (#182)
* Use bytes. Not yet zerocopy everywhere but compiles * Actually zerocopy * Actually zerocopy * Not actually storing the torrent on disk now
This commit is contained in:
parent
3cc9e444b1
commit
c7ed475f54
20 changed files with 182 additions and 95 deletions
|
|
@ -6,12 +6,13 @@
|
|||
// This lets us express types like TorrentMetaInfo<&[u8]> for zero-copy metadata about a bencode buffer in memory,
|
||||
// but to have one-line conversion for it into TorrentMetaInfo<Vec<u8>> so that we can store it later somewhere.
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait CloneToOwned {
|
||||
type Target;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target;
|
||||
fn clone_to_owned(&self, within_buffer: Option<&Bytes>) -> Self::Target;
|
||||
}
|
||||
|
||||
impl<T> CloneToOwned for Option<T>
|
||||
|
|
@ -20,8 +21,8 @@ where
|
|||
{
|
||||
type Target = Option<<T as CloneToOwned>::Target>;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target {
|
||||
self.as_ref().map(|i| i.clone_to_owned())
|
||||
fn clone_to_owned(&self, within_buffer: Option<&Bytes>) -> Self::Target {
|
||||
self.as_ref().map(|i| i.clone_to_owned(within_buffer))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -31,15 +32,17 @@ where
|
|||
{
|
||||
type Target = Vec<<T as CloneToOwned>::Target>;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target {
|
||||
self.iter().map(|i| i.clone_to_owned()).collect()
|
||||
fn clone_to_owned(&self, within_buffer: Option<&Bytes>) -> Self::Target {
|
||||
self.iter()
|
||||
.map(|i| i.clone_to_owned(within_buffer))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl CloneToOwned for u8 {
|
||||
type Target = u8;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target {
|
||||
fn clone_to_owned(&self, _within_buffer: Option<&Bytes>) -> Self::Target {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +50,7 @@ impl CloneToOwned for u8 {
|
|||
impl CloneToOwned for u32 {
|
||||
type Target = u32;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target {
|
||||
fn clone_to_owned(&self, _within_buffer: Option<&Bytes>) -> Self::Target {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
|
@ -60,10 +63,13 @@ where
|
|||
{
|
||||
type Target = HashMap<<K as CloneToOwned>::Target, <V as CloneToOwned>::Target>;
|
||||
|
||||
fn clone_to_owned(&self) -> Self::Target {
|
||||
fn clone_to_owned(&self, within_buffer: Option<&Bytes>) -> Self::Target {
|
||||
let mut result = HashMap::with_capacity(self.capacity());
|
||||
for (k, v) in self {
|
||||
result.insert(k.clone_to_owned(), v.clone_to_owned());
|
||||
result.insert(
|
||||
k.clone_to_owned(within_buffer),
|
||||
v.clone_to_owned(within_buffer),
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue