string
string copied to clipboard
Fix `from_str` for more generic usage.
The previous from_str
require T meets From<&'a [u8]>
, which is not a common case for many types.
pub fn from_str(src: &str) -> String<T>
where
T: From<&'a [u8]> + StableAsRef,
{}
For example, the Bytes
only implements from &'static [u8]
and &'static str
. In the previous String::from_str
impl, it's impossible to build a String<Bytes>
from a non-static &str
.
let s = std::string::String::from("foo");
let _: String<Bytes> = String::from_str(s.as_str()); // cannot compiled
A FromIterator<u8>
is more generic IMO, but with a little breaking changes.