From f0378d13f9a86c241f8215bb028fd4c2db8232a3 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Tue, 3 Oct 2023 19:29:30 +1000 Subject: [PATCH] mark more ctors as const --- fmt/src/to_value.rs | 8 ++++---- json/src/value.rs | 4 ++-- serde/src/to_serialize.rs | 4 ++-- serde/src/to_value.rs | 4 ++-- src/data/binary.rs | 8 ++++---- src/data/map.rs | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/fmt/src/to_value.rs b/fmt/src/to_value.rs index 01bd9f41..1744c60a 100644 --- a/fmt/src/to_value.rs +++ b/fmt/src/to_value.rs @@ -30,7 +30,7 @@ impl DebugToValue { /** Adapt a [`fmt::Debug`] into an [`sval::Value`]. */ - pub fn new(value: V) -> DebugToValue { + pub const fn new(value: V) -> DebugToValue { DebugToValue(value) } } @@ -39,7 +39,7 @@ impl DebugToValue { /** Adapt a reference to a [`fmt::Debug`] into an [`sval::Value`]. */ - pub fn new_borrowed<'a>(value: &'a V) -> &'a DebugToValue { + pub const fn new_borrowed<'a>(value: &'a V) -> &'a DebugToValue { // SAFETY: `&'a V` and `&'a DebugToValue` have the same ABI unsafe { &*(value as *const _ as *const DebugToValue) } } @@ -55,7 +55,7 @@ impl DisplayToValue { /** Adapt a [`fmt::Display`] into an [`sval::Value`]. */ - pub fn new(value: V) -> DisplayToValue { + pub const fn new(value: V) -> DisplayToValue { DisplayToValue(value) } } @@ -64,7 +64,7 @@ impl DisplayToValue { /** Adapt a reference to a [`fmt::Display`] into an [`sval::Value`]. */ - pub fn new_borrowed<'a>(value: &'a V) -> &'a DisplayToValue { + pub const fn new_borrowed<'a>(value: &'a V) -> &'a DisplayToValue { // SAFETY: `&'a V` and `&'a DisplayToValue` have the same ABI unsafe { &*(value as *const _ as *const DisplayToValue) } } diff --git a/json/src/value.rs b/json/src/value.rs index 1a9744b7..bcd9cba4 100644 --- a/json/src/value.rs +++ b/json/src/value.rs @@ -14,7 +14,7 @@ impl JsonStr { /** Treat a string as native JSON. */ - pub fn new<'a>(json: &'a str) -> &'a Self { + pub const fn new<'a>(json: &'a str) -> &'a Self { // SAFETY: `JsonStr` and `str` have the same ABI unsafe { &*(json as *const _ as *const JsonStr) } } @@ -22,7 +22,7 @@ impl JsonStr { /** Get a reference to the underlying string. */ - pub fn as_str(&self) -> &str { + pub const fn as_str(&self) -> &str { &self.0 } } diff --git a/serde/src/to_serialize.rs b/serde/src/to_serialize.rs index 212ce102..dc442581 100644 --- a/serde/src/to_serialize.rs +++ b/serde/src/to_serialize.rs @@ -28,7 +28,7 @@ impl ToSerialize { /** Adapt an [`sval::Value`] into a [`serde::Serialize`]. */ - pub fn new(value: V) -> ToSerialize { + pub const fn new(value: V) -> ToSerialize { ToSerialize(value) } } @@ -37,7 +37,7 @@ impl ToSerialize { /** Adapt a reference to an [`sval::Value`] into a [`serde::Serialize`]. */ - pub fn new_borrowed<'a>(value: &'a V) -> &'a ToSerialize { + pub const fn new_borrowed<'a>(value: &'a V) -> &'a ToSerialize { // SAFETY: `&'a V` and `&'a ToSerialize` have the same ABI unsafe { &*(value as *const _ as *const ToSerialize) } } diff --git a/serde/src/to_value.rs b/serde/src/to_value.rs index ecae31ec..50bedb1b 100644 --- a/serde/src/to_value.rs +++ b/serde/src/to_value.rs @@ -22,7 +22,7 @@ impl ToValue { /** Adapt a [`serde::Serialize`] into a [`sval::Value`]. */ - pub fn new(value: V) -> ToValue { + pub const fn new(value: V) -> ToValue { ToValue(value) } } @@ -31,7 +31,7 @@ impl ToValue { /** Adapt a reference to a [`serde::Serialize`] into an [`sval::Value`]. */ - pub fn new_borrowed<'a>(value: &'a V) -> &'a ToValue { + pub const fn new_borrowed<'a>(value: &'a V) -> &'a ToValue { // SAFETY: `&'a V` and `&'a ToValue` have the same ABI unsafe { &*(value as *const _ as *const ToValue) } } diff --git a/src/data/binary.rs b/src/data/binary.rs index 62c951dc..da5bb0b0 100644 --- a/src/data/binary.rs +++ b/src/data/binary.rs @@ -13,7 +13,7 @@ impl BinarySlice { Treat a slice of 8bit unsigned integers as binary. */ #[inline(always)] - pub fn new<'a>(binary: &'a [u8]) -> &'a Self { + pub const fn new<'a>(binary: &'a [u8]) -> &'a Self { // SAFETY: `Binary` and `[u8]` have the same ABI unsafe { &*(binary as *const _ as *const BinarySlice) } } @@ -22,7 +22,7 @@ impl BinarySlice { Get a reference to the underlying slice. */ #[inline(always)] - pub fn as_slice(&self) -> &[u8] { + pub const fn as_slice(&self) -> &[u8] { &self.0 } } @@ -66,7 +66,7 @@ impl BinaryArray { Treat a slice of 8bit unsigned integers as binary. */ #[inline(always)] - pub fn new<'a>(binary: &'a [u8; N]) -> &'a Self { + pub const fn new<'a>(binary: &'a [u8; N]) -> &'a Self { // SAFETY: `Binary` and `[u8; N]` have the same ABI unsafe { &*(binary as *const _ as *const BinaryArray) } } @@ -75,7 +75,7 @@ impl BinaryArray { Get a reference to the underlying slice. */ #[inline(always)] - pub fn as_slice(&self) -> &[u8; N] { + pub const fn as_slice(&self) -> &[u8; N] { &self.0 } } diff --git a/src/data/map.rs b/src/data/map.rs index fd96e0f5..131fc437 100644 --- a/src/data/map.rs +++ b/src/data/map.rs @@ -10,7 +10,7 @@ impl MapSlice { /** Treat a slice of key-value pairs as a map. */ - pub fn new<'a>(map: &'a [(K, V)]) -> &'a Self { + pub const fn new<'a>(map: &'a [(K, V)]) -> &'a Self { // SAFETY: `MapSlice` and `[(K, V)]` have the same ABI unsafe { &*(map as *const _ as *const MapSlice) } } @@ -18,7 +18,7 @@ impl MapSlice { /** Get a reference to the underlying slice. */ - pub fn as_slice(&self) -> &[(K, V)] { + pub const fn as_slice(&self) -> &[(K, V)] { &self.0 } }