Skip to content

Commit

Permalink
feat: remove from_maybe_shared constructors
Browse files Browse the repository at this point in the history
This also replaces `from_maybe_shared_unchecked` with the new
`from_shared_unchecked` constructor.

BREAKING CHANGE: the `from_maybe_shared` constructors are removed. Use
`TryFrom<Bytes>::try_from` instead.
  • Loading branch information
tesaguri committed Aug 11, 2023
1 parent d84fed0 commit 9ceb094
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 92 deletions.
17 changes: 0 additions & 17 deletions src/convert.rs

This file was deleted.

30 changes: 3 additions & 27 deletions src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,46 +172,22 @@ impl HeaderValue {
HeaderValue::try_from_generic(src, Bytes::copy_from_slice)
}

/// Attempt to convert a `Bytes` buffer to a `HeaderValue`.
///
/// This will try to prevent a copy if the type passed is the type used
/// internally, and will copy the data if it is not.
pub fn from_maybe_shared<T>(src: T) -> Result<HeaderValue, InvalidHeaderValue>
where
T: AsRef<[u8]> + 'static,
{
if_downcast_into!(T, Bytes, src, {
return HeaderValue::try_from(src);
});

HeaderValue::from_bytes(src.as_ref())
}

/// Convert a `Bytes` directly into a `HeaderValue` without validating.
///
/// This function does NOT validate that illegal bytes are not contained
/// within the buffer.
pub unsafe fn from_maybe_shared_unchecked<T>(src: T) -> HeaderValue
pub unsafe fn from_shared_unchecked(src: Bytes) -> HeaderValue
where
T: AsRef<[u8]> + 'static,
{
if cfg!(debug_assertions) {
match HeaderValue::from_maybe_shared(src) {
match HeaderValue::try_from(src) {
Ok(val) => val,
Err(_err) => {
panic!("HeaderValue::from_maybe_shared_unchecked() with invalid bytes");
panic!("HeaderValue::from_shared_unchecked() with invalid bytes");
}
}
} else {

if_downcast_into!(T, Bytes, src, {
return HeaderValue {
inner: src,
is_sensitive: false,
};
});

let src = Bytes::copy_from_slice(src.as_ref());
HeaderValue {
inner: src,
is_sensitive: false,
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ extern crate doc_comment;
#[cfg(test)]
doctest!("../README.md");

#[macro_use]
mod convert;

pub mod header;
pub mod method;
pub mod request;
Expand Down
15 changes: 0 additions & 15 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ impl Authority {
.expect("static str is not valid authority")
}

/// Attempt to convert a `Bytes` buffer to a `Authority`.
///
/// This will try to prevent a copy if the type passed is the type used
/// internally, and will copy the data if it is not.
pub fn from_maybe_shared<T>(src: T) -> Result<Self, InvalidUri>
where
T: AsRef<[u8]> + 'static,
{
if_downcast_into!(T, Bytes, src, {
return Authority::try_from(src);
});

Authority::try_from(src.as_ref())
}

// Note: this may return an *empty* Authority. You might want `parse_non_empty`.
// Postcondition: for all Ok() returns, s[..ret.unwrap()] is valid UTF-8 where
// ret is the return value.
Expand Down
15 changes: 0 additions & 15 deletions src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,6 @@ impl Uri {
})
}

/// Attempt to convert a `Bytes` buffer to a `Uri`.
///
/// This will try to prevent a copy if the type passed is the type used
/// internally, and will copy the data if it is not.
pub fn from_maybe_shared<T>(src: T) -> Result<Self, InvalidUri>
where
T: AsRef<[u8]> + 'static,
{
if_downcast_into!(T, Bytes, src, {
return Uri::try_from(src);
});

Uri::try_from(src.as_ref())
}

/// Convert a `Uri` from a static string.
///
/// This function will not perform any copying, however the string is
Expand Down
15 changes: 0 additions & 15 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@ impl PathAndQuery {
PathAndQuery::try_from(src).unwrap()
}

/// Attempt to convert a `Bytes` buffer to a `PathAndQuery`.
///
/// This will try to prevent a copy if the type passed is the type used
/// internally, and will copy the data if it is not.
pub fn from_maybe_shared<T>(src: T) -> Result<Self, InvalidUri>
where
T: AsRef<[u8]> + 'static,
{
if_downcast_into!(T, Bytes, src, {
return PathAndQuery::try_from(src);
});

PathAndQuery::try_from(src.as_ref())
}

pub(super) fn empty() -> Self {
PathAndQuery {
data: ByteStr::new(),
Expand Down

0 comments on commit 9ceb094

Please sign in to comment.