use std::{convert::Infallible, error::Error as StdError, mem::size_of_val};
use bytes::Bytes;
use http_body_util::{BodyExt, Empty, Full};
use kynos::{Error, openapi::Violation};
type BoxError = Box<dyn StdError + Send + Sync>;
#[test]
fn a_build_failure_does_not_inline_a_violation() {
let error = size_of::<Error>();
let violation = size_of::<Violation>();
assert!(
error < violation,
"Error ({error} bytes) should not inline Violation ({violation} bytes); \
the violation list must stay behind a Vec"
);
assert!(
error <= 64,
"Error grew to {error} bytes; box the payload of any wide variant that was added"
);
}
#[test]
fn a_build_result_costs_no_more_than_its_failure() {
let result = size_of::<kynos::Result<()>>();
let error = size_of::<Error>();
assert_eq!(
result, error,
"Result<(), Error> ({result} bytes) should be no wider than Error ({error} bytes); \
the Ok/Err discriminant must fit Error's own tag niche"
);
}
#[test]
fn an_empty_body_erases_a_zero_sized_type() {
let erased = Empty::<Bytes>::new().map_err(|never: Infallible| -> BoxError { match never {} });
let size = size_of_val(&erased);
assert_eq!(
size, 0,
"the body `Body::empty` erases is {size} bytes rather than zero, so \
boxing it now allocates; the zero recorded in tests/alloc_body.rs is \
no longer free and docs/architecture.md's verdict on it is stale"
);
}
#[test]
fn a_body_holding_bytes_is_not_zero_sized() {
let erased = Full::new(Bytes::from_static(b"{\"ok\":true}"))
.map_err(|never: Infallible| -> BoxError { match never {} });
assert!(
size_of_val(&erased) > 0,
"a body carrying bytes is zero-sized, which would make the allocation \
recorded for `Body::from_bytes` something other than the boxing"
);
}
#[test]
fn the_body_the_server_erases_is_not_zero_sized() {
let size = size_of::<hyper::body::Incoming>();
assert!(
size > 0,
"hyper's incoming body is zero-sized, so erasing it would be free and \
the server path would cost one allocation fewer per request than \
docs/architecture.md records"
);
}