From ec0825fe194cdb26a7f0b142d552d10ca8f65e76 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Thu, 9 Jan 2020 22:27:28 +0000 Subject: [PATCH] Make Bytes::new const fn (#356) --- src/bytes.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bytes.rs b/src/bytes.rs index 9e4dd91..93ab84b 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -96,8 +96,18 @@ impl Bytes { /// assert_eq!(&b[..], b""); /// ``` #[inline] + #[cfg(not(all(loom, test)))] + pub const fn new() -> Bytes { + // Make it a named const to work around + // "unsizing casts are not allowed in const fn" + const EMPTY: &[u8] = &[]; + Bytes::from_static(EMPTY) + } + + #[cfg(all(loom, test))] pub fn new() -> Bytes { - Bytes::from_static(b"") + const EMPTY: &[u8] = &[]; + Bytes::from_static(EMPTY) } /// Creates a new `Bytes` from a static slice.