Skip to content

Commit

Permalink
Use ArrayVec from hoot
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Oct 12, 2024
1 parent 01da37a commit 979152f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 64 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _test = []

[dependencies]
base64 = "0.22.1"
hoot = "0.2.3"
hoot = "0.2.4"
# hoot = { path = "../hoot/hoot" }
http = "1.1.0"
log = "0.4.22"
Expand Down
62 changes: 2 additions & 60 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::convert::TryFrom;
use std::fmt;
use std::io::{self, ErrorKind};
use std::ops::{Deref, DerefMut};

use http::uri::{Authority, Scheme};
use http::{HeaderMap, HeaderValue, Method, Response, Uri, Version};

use crate::proxy::Proto;
use crate::Error;

pub use hoot::ArrayVec;

pub(crate) mod private {
pub trait Private {}
}
Expand Down Expand Up @@ -356,62 +357,3 @@ impl HeaderMapExt for HeaderMap {
self.contains_key("accept")
}
}

pub struct ArrayVec<T, const N: usize> {
len: usize,
arr: [T; N],
}

impl<T, const N: usize> Deref for ArrayVec<T, N> {
type Target = [T];

fn deref(&self) -> &Self::Target {
&self.arr[..self.len]
}
}

impl<T, const N: usize> DerefMut for ArrayVec<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.arr[..self.len]
}
}

impl<T, const N: usize> ArrayVec<T, N> {
pub fn from_fn(cb: impl FnMut(usize) -> T) -> Self {
Self {
len: 0,
arr: std::array::from_fn(cb),
}
}

pub fn push(&mut self, value: T) {
self.arr[self.len] = value;
self.len += 1;
}

pub fn truncate(&mut self, len: usize) {
assert!(len <= self.len);
self.len = len;
}
}

impl<T, const N: usize> fmt::Debug for ArrayVec<T, N>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ArrayVec")
.field("len", &self.len)
.field("arr", &self.arr)
.finish()
}
}

impl<'a, T, const N: usize> IntoIterator for &'a ArrayVec<T, N> {
type Item = &'a T;
type IntoIter = core::slice::Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self[..self.len].iter()
}
}

0 comments on commit 979152f

Please sign in to comment.