From 0a533b6ab6750ee787a8cd9ed6f89acc4ec981de Mon Sep 17 00:00:00 2001 From: Weiyuan Wu Date: Wed, 10 Jan 2024 15:36:24 -0800 Subject: [PATCH] support bytemuck --- Cargo.toml | 1 + src/decimal.rs | 13 ++++++++++++- src/lib.rs | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5bf7199..149448a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ rustdoc-args = ["--cfg", "docsrs"] arbitrary = { default-features = false, optional = true, version = "1.0" } arrayvec = { default-features = false, version = "0.7" } borsh = { default-features = false, features = ["derive", "unstable__schema"], optional = true, version = "1.1.1" } +bytemuck = { default-features = false, optional = true, version = "1" } bytes = { default-features = false, optional = true, version = "1.0" } diesel1 = { default-features = false, optional = true, package = "diesel", version = "1.0" } diesel2 = { default-features = false, optional = true, package = "diesel", version = "2.1" } diff --git a/src/decimal.rs b/src/decimal.rs index 45f5661..c842578 100644 --- a/src/decimal.rs +++ b/src/decimal.rs @@ -13,6 +13,9 @@ use core::{ str::FromStr, }; +#[cfg(feature = "bytemuck")] +use bytemuck::{Pod, Zeroable}; + // Diesel configuration #[cfg(feature = "diesel2")] use diesel::deserialize::FromSqlRow; @@ -109,7 +112,7 @@ pub struct UnpackedDecimal { sql_type = "Numeric" )] #[cfg_attr(feature = "diesel2", derive(FromSqlRow, AsExpression), diesel(sql_type = Numeric))] -#[cfg_attr(feature = "c-repr", repr(C))] +#[cfg_attr(any(feature = "c-repr", feature = "bytemuck"), repr(C))] #[cfg_attr( feature = "borsh", derive(borsh::BorshDeserialize, borsh::BorshSerialize, borsh::BorshSchema) @@ -134,6 +137,14 @@ pub struct Decimal { mid: u32, } +#[cfg(feature = "bytemuck")] +#[allow(unsafe_code)] +unsafe impl Pod for Decimal {} + +#[cfg(feature = "bytemuck")] +#[allow(unsafe_code)] +unsafe impl Zeroable for Decimal {} + #[cfg(feature = "ndarray")] impl ndarray::ScalarOperand for Decimal {} diff --git a/src/lib.rs b/src/lib.rs index cb3c7dc..ce322a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![doc = include_str!(concat!(env!("OUT_DIR"), "/README-lib.md"))] -#![forbid(unsafe_code)] +#![cfg_attr(not(feature = "bytemuck"), forbid(unsafe_code))] +#![cfg_attr(feature = "bytemuck", deny(unsafe_code))] #![deny(clippy::print_stdout, clippy::print_stderr)] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(docsrs, feature(doc_auto_cfg))]