Skip to content

Commit

Permalink
fix: use ref_cast replace transmute
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Nov 15, 2024
1 parent 848749a commit 7a7ad92
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bytes = "1.8"
cfg-if = "1.0"
faststr = { version = "0.2", features = ["serde"] }
itoa = "1.0"
ref-cast = "1.0"
ryu = "1.0"
serde = { version = "1.0", features = ["rc", "derive"] }
simdutf8 = "0.1"
Expand Down
14 changes: 7 additions & 7 deletions sonic-number/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
authors = ["Volo Team <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
name = "sonic-number"
description = "Fast number parsing based on SIMD"
repository = "https://github.com/cloudwego/sonic-rs"
version = "0.1.0"
authors = ["Volo Team <[email protected]>"]
description = "Fast number parsing based on SIMD"
edition = "2021"
license = "Apache-2.0"
name = "sonic-number"
repository = "https://github.com/cloudwego/sonic-rs"
version = "0.1.0"


[dependencies]
Expand Down
15 changes: 7 additions & 8 deletions sonic-simd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
authors = ["Volo Team <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
description = "Portable SIMD API for sonic-rs"
name = "sonic-simd"
repository = "https://github.com/cloudwego/sonic-rs"
version = "0.1.0"

authors = ["Volo Team <[email protected]>"]
description = "Portable SIMD API for sonic-rs"
edition = "2021"
license = "Apache-2.0"
name = "sonic-simd"
repository = "https://github.com/cloudwego/sonic-rs"
version = "0.1.0"


[dependencies]
Expand Down
4 changes: 3 additions & 1 deletion src/value/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
slice::{from_raw_parts, from_raw_parts_mut},
};

use ref_cast::RefCast;

use super::node::ValueMut;
use crate::{
serde::tri,
Expand All @@ -30,7 +32,7 @@ use crate::{
/// let j = sonic_rs::json!([1, 2, 3]);
/// assert_eq!(j.as_array().unwrap()[0], 1);
/// ```
#[derive(Debug, Eq, PartialEq, Clone)]
#[derive(Debug, Eq, PartialEq, Clone, RefCast)]
#[repr(transparent)]
pub struct Array(pub(crate) Value);

Expand Down
9 changes: 5 additions & 4 deletions src/value/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{

use bumpalo::Bump;
use faststr::FastStr;
use ref_cast::RefCast;
use serde::{
ser::{Serialize, SerializeMap, SerializeSeq, SerializeStruct},
Serializer,
Expand Down Expand Up @@ -829,7 +830,7 @@ impl JsonContainerTrait for Value {
#[inline]
fn as_array(&self) -> Option<&Self::ArrayType> {
if self.is_array() {
Some(unsafe { transmute::<&Self, &Self::ArrayType>(self) })
Some(Self::ArrayType::ref_cast(self))
} else {
None
}
Expand All @@ -838,7 +839,7 @@ impl JsonContainerTrait for Value {
#[inline]
fn as_object(&self) -> Option<&Self::ObjectType> {
if self.is_object() {
Some(unsafe { transmute::<&Self, &Self::ObjectType>(self) })
Some(Self::ObjectType::ref_cast(self))
} else {
None
}
Expand All @@ -854,7 +855,7 @@ impl JsonValueMutTrait for Value {
fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType> {
if self.is_object() {
self.to_mut();
Some(unsafe { transmute::<&mut Self, &mut Self::ObjectType>(self) })
Some(Self::ObjectType::ref_cast_mut(self))
} else {
None
}
Expand All @@ -864,7 +865,7 @@ impl JsonValueMutTrait for Value {
fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType> {
if self.is_array() {
self.to_mut();
Some(unsafe { transmute::<&mut Self, &mut Self::ArrayType>(self) })
Some(Self::ArrayType::ref_cast_mut(self))
} else {
None
}
Expand Down
4 changes: 3 additions & 1 deletion src/value/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Represents a parsed JSON object.
use std::{iter::FusedIterator, marker::PhantomData, slice};

use ref_cast::RefCast;

use super::{node::ValueMut, value_trait::JsonValueTrait};
use crate::{serde::tri, util::reborrow::DormantMutRef, value::node::Value};

Expand Down Expand Up @@ -34,7 +36,7 @@ use crate::{serde::tri, util::reborrow::DormantMutRef, value::node::Value};
/// ```
/// If you care about that, recommend to use `HashMap` or `BTreeMap` instead. The parse performance
/// is slower than `Object`.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, RefCast)]
#[repr(transparent)]
pub struct Object(pub(crate) Value);

Expand Down

0 comments on commit 7a7ad92

Please sign in to comment.