string icon indicating copy to clipboard operation
string copied to clipboard

Fix `from_str` for more generic usage.

Open belltoy opened this issue 2 years ago • 0 comments

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.

belltoy avatar Apr 21 '22 04:04 belltoy