-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
impl Challenge for &'static str
#248
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Changes | ||
|
||
## Unreleased - 2021-xx-xx | ||
- impl Challenge for &str | ||
|
||
|
||
## 0.6.0 - 2022-03-01 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,27 @@ pub trait Challenge: TryIntoHeaderValue + Debug + Display + Clone + Send + Sync | |
/// Converts the challenge into a bytes suitable for HTTP transmission. | ||
fn to_bytes(&self) -> Bytes; | ||
} | ||
|
||
/// This is particularly useful for writing constructs such as | ||
/// `AuthenticationError::new("Authentication required")` | ||
impl Challenge for &'static str { | ||
fn to_bytes(&self) -> Bytes { | ||
(*self).into() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use test_strategy::proptest; | ||
|
||
use super::*; | ||
|
||
#[proptest] | ||
fn roundtrip_static_str(input: Box<str>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that conversion really needs to use such expensive testing, isn't it enough to add a basic test? We should be cautious to add an additional test framework just for one test. |
||
// This will leak, but it's probably fine in the context of a test. Fixable by adding: | ||
// unsafe { Box::from_raw(s as *const str as *mut str); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unleaking is UB, especially through a shared reference. This should use |
||
let s: &'static str = Box::leak(input); | ||
let bytes = s.to_bytes(); | ||
assert_eq!(s.as_bytes(), bytes); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.