Closed morrisonlevi closed 6 months ago
Failure is:
error: package `half v2.3.1` cannot be built because it requires rustc 1.70 or newer, while the currently active rustc version is 1.65.0
Either upgrade to rustc 1.70 or newer, or use
cargo update -p half@2.3.1 --precise ver
where `ver` is the latest version of `half` supporting rustc 1.65.0
Error: Process completed with exit code 101.
This PR didn't touch any of that stuff, obviously. In order to run tests locally, I had to use Rust 1.70.
Hello @morrisonlevi,
I'm interested in this feature, too, for str
and [u8]
slices. Two questions:
string::encode
directly as fn encode<S, B>(tag: u32, value: &impl AsRef<str>, buf: &mut B)
?For bytes, all we have to do is:
AsRef<[u8]>
constraint on BytesAdapter
: trait BytesAdapter: Default + Sized + AsRef<[u8]> + 'static
BytesAdapter::append_to
bytes::encode
as:pub fn encode<A, B>(tag: u32, value: &A, buf: &mut B)
where
A: AsRef<[u8]>,
B: BufMut,
{
let bytes = value.as_ref();
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(bytes.len() as u64, buf);
buf.put_slice(bytes);
}
When I tried changing encode
directly, tests would fail because of mismatches in expected types in a bunch of test helpers. It was easier to add this helper and keep the tests green.
@guilload If your tests are passing, feel free to open a PR and I'll close this one.
Ok. I opened #979. CI is failing because of the MSRV issue, but tests are passing locally.
This allows users to encode structs that have types like
&str
,Box<str>
,Cow<str>
, etc if they are willing to get their hands dirty. This more efficient than creating a parallel type that holds aString
, copying it over, just for the purpose of encoding it, then throwing it away.Ideally we'd support types like
Cow<'a, str>
directly but there are plenty of issues and challenges with that. I am willing to get my hands dirty, but I prefer having this helper in prost rather than copying the relevant bits into my own project.