From 0f65a0df8a88efbf4e4fb46788c036fdb9e16899 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Thu, 5 Dec 2019 11:48:14 +0100 Subject: [PATCH] Add octets traits for SmallVec. --- domain-core/Cargo.toml | 12 ++++--- domain-core/src/octets.rs | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/domain-core/Cargo.toml b/domain-core/Cargo.toml index 41b184b8..6f54c5de 100644 --- a/domain-core/Cargo.toml +++ b/domain-core/Cargo.toml @@ -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"] diff --git a/domain-core/src/octets.rs b/domain-core/src/octets.rs index ca737942..c542261f 100644 --- a/domain-core/src/octets.rs +++ b/domain-core/src/octets.rs @@ -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> OctetsExt for SmallVec { + 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> OctetsRef for &'a SmallVec { + 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> OctetsBuilder for SmallVec { + 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> EmptyBuilder for SmallVec { + 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> IntoOctets for SmallVec { + type Octets = Self; + + fn into_octets(self) -> Self::Octets { + self + } +} + //------------ IntoBuilder --------------------------------------------------- @@ -281,6 +330,15 @@ impl IntoBuilder for Bytes { } } +#[cfg(feature = "smallvec")] +impl> IntoBuilder for SmallVec { + type Builder = Self; + + fn into_builder(self) -> Self::Builder { + self + } +} + //------------ FromBuilder --------------------------------------------------- @@ -308,6 +366,15 @@ impl FromBuilder for Bytes { } } +#[cfg(feature = "smallvec")] +impl> FromBuilder for SmallVec { + type Builder = Self; + + fn from_builder(builder: Self) -> Self { + builder + } +} + //------------ Compose -------------------------------------------------------