I'm writing a function that needs to be able to take either a string literal or a bytes literal as an argument, and this argument will be passed to a function from a third-party library that takes a &'static [u8]
. The best code I can come up with is of the form:
fn static_taker(_bytes: &'static [u8]) {
todo!()
}
fn call_static_taker<S: AsRef<[u8]> + 'static>(bs: S) {
static_taker(bs.as_ref());
}
fn call_1() {
call_static_taker("This is a &'static str.");
}
fn call_2() {
call_static_taker(&b"This is a &'static [u8]."[..]);
}
but this fails to compile with:
Compiling playground v0.0.1 (/playground)
error[E0597]: `bs` does not live long enough
--> src/lib.rs:6:18
|
6 | static_taker(bs.as_ref());
| -------------^^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `bs` is borrowed for `'static`
7 | }
| - `bs` dropped here while still borrowed
For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground` due to previous error
How do I get this to compile?