Add octets traits for SmallVec.

This commit is contained in:
Martin Hoffmann
2019-12-05 11:48:14 +01:00
parent 5c3479236b
commit 0f65a0df8a
2 changed files with 74 additions and 5 deletions
+7 -5
View File
@@ -16,12 +16,14 @@ name = "domain_core"
path = "src/lib.rs"
[dependencies]
chrono = "0.4"
derive_more = "0.99.1"
rand = "0.7"
unwrap = "1.2"
void = "1.0"
bytes = { version = "^0.4", optional = true }
chrono = { version = "^0.4", optional = true }
derive_more = { version = "0.15.1-pre", git = "https://github.com/JelteF/derive_more.git" }
rand = "^0.7"
unwrap = "^1.2"
void = "^1.0"
smallvec = { version = "1.0", optional = true }
[features]
default = ["std"]
+67
View File
@@ -5,6 +5,7 @@ use core::cmp::Ordering;
use core::convert::TryFrom;
#[cfg(feature = "std")] use std::vec::Vec;
#[cfg(feature = "bytes")] use bytes::{Bytes, BytesMut};
#[cfg(feature = "smallvec")] use smallvec::{Array, SmallVec};
use derive_more::Display;
use crate::name::ToDname;
use crate::net::{Ipv4Addr, Ipv6Addr};
@@ -37,6 +38,13 @@ impl OctetsExt for Bytes {
}
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> OctetsExt for SmallVec<A> {
fn truncate(&mut self, len: usize) {
self.truncate(len)
}
}
//------------ OctetsRef -----------------------------------------------------
@@ -91,6 +99,15 @@ impl<'a> OctetsRef for &'a Bytes {
}
}
#[cfg(feature = "smallvec")]
impl<'a, A: Array<Item = u8>> OctetsRef for &'a SmallVec<A> {
type Range = &'a [u8];
fn range(self, start: usize, end: usize) -> Self::Range {
&self.as_slice()[start..end]
}
}
//------------ OctetsBuilder -------------------------------------------------
@@ -187,6 +204,18 @@ impl OctetsBuilder for BytesMut {
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> OctetsBuilder for SmallVec<A> {
fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf> {
self.extend_from_slice(slice);
Ok(())
}
fn truncate(&mut self, len: usize) {
SmallVec::truncate(self, len)
}
}
//------------ EmptyBuilder --------------------------------------------------
@@ -218,6 +247,17 @@ impl EmptyBuilder for BytesMut {
}
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> EmptyBuilder for SmallVec<A> {
fn empty() -> Self {
SmallVec::new()
}
fn with_capacity(capacity: usize) -> Self {
SmallVec::with_capacity(capacity)
}
}
//------------ IntoOctets ----------------------------------------------------
@@ -245,6 +285,15 @@ impl IntoOctets for BytesMut {
}
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> IntoOctets for SmallVec<A> {
type Octets = Self;
fn into_octets(self) -> Self::Octets {
self
}
}
//------------ IntoBuilder ---------------------------------------------------
@@ -281,6 +330,15 @@ impl IntoBuilder for Bytes {
}
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> IntoBuilder for SmallVec<A> {
type Builder = Self;
fn into_builder(self) -> Self::Builder {
self
}
}
//------------ FromBuilder ---------------------------------------------------
@@ -308,6 +366,15 @@ impl FromBuilder for Bytes {
}
}
#[cfg(feature = "smallvec")]
impl<A: Array<Item = u8>> FromBuilder for SmallVec<A> {
type Builder = Self;
fn from_builder(builder: Self) -> Self {
builder
}
}
//------------ Compose -------------------------------------------------------