2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2019-07-26 21:54:25 +00:00
|
|
|
|
2018-09-17 09:18:35 +00:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
2014-04-03 04:31:00 +00:00
|
|
|
|
2014-12-12 18:59:41 +00:00
|
|
|
use std::fmt;
|
2020-07-04 15:20:24 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::Cursor;
|
2014-07-29 23:31:39 +00:00
|
|
|
use std::slice;
|
2022-05-05 16:34:13 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
trait Encoder {
|
|
|
|
type Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Encodable<S: Encoder> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsonEncoder<'a>(PhantomData<&'a mut ()>);
|
|
|
|
|
|
|
|
impl Encoder for JsonEncoder<'_> {
|
|
|
|
type Error = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AsJson<'a, T> {
|
|
|
|
inner: &'a T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: for<'r> Encodable<JsonEncoder<'r>>> fmt::Display for AsJson<'a, T> {
|
|
|
|
/// Encodes a json value into a string
|
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_json<T>(t: &T) -> AsJson<'_, T> {
|
|
|
|
AsJson { inner: t }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OpaqueEncoder(Vec<u8>);
|
|
|
|
|
|
|
|
impl Encoder for OpaqueEncoder {
|
|
|
|
type Error = ();
|
|
|
|
}
|
2014-07-29 23:31:39 +00:00
|
|
|
|
2014-04-03 04:31:00 +00:00
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
baz: bool,
|
|
|
|
}
|
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
impl<S: Encoder> Encodable<S> for Foo {
|
|
|
|
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 04:31:00 +00:00
|
|
|
struct Bar {
|
2015-03-26 00:06:52 +00:00
|
|
|
froboz: usize,
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
impl<S: Encoder> Encodable<S> for Bar {
|
|
|
|
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 04:31:00 +00:00
|
|
|
enum WireProtocol {
|
|
|
|
JSON,
|
2016-09-16 14:25:54 +00:00
|
|
|
Opaque,
|
2014-04-03 04:31:00 +00:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
fn encode_json<T: for<'a> Encodable<JsonEncoder<'a>>>(val: &T, wr: &mut Cursor<Vec<u8>>) {
|
|
|
|
write!(wr, "{}", as_json(val));
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 03:30:45 +00:00
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
fn encode_opaque<T: Encodable<OpaqueEncoder>>(val: &T, wr: Vec<u8>) {
|
|
|
|
let mut encoder = OpaqueEncoder(wr);
|
2014-04-03 04:31:00 +00:00
|
|
|
val.encode(&mut encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2020-07-04 15:20:24 +00:00
|
|
|
let target = Foo { baz: false };
|
2014-11-06 08:05:53 +00:00
|
|
|
let proto = WireProtocol::JSON;
|
2014-04-03 04:31:00 +00:00
|
|
|
match proto {
|
2018-06-04 20:14:02 +00:00
|
|
|
WireProtocol::JSON => encode_json(&target, &mut Cursor::new(Vec::new())),
|
2020-07-04 15:20:24 +00:00
|
|
|
WireProtocol::Opaque => encode_opaque(&target, Vec::new()),
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
|
|
|
}
|