Skip to content

Commit

Permalink
refactor: rewrite NodeSeq
Browse files Browse the repository at this point in the history
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
  • Loading branch information
woshiluo committed Oct 31, 2024
1 parent 55143de commit 6b98e91
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 421 deletions.
61 changes: 3 additions & 58 deletions src/de_mut/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub(super) struct AnyCursor<T: Type = Body>(usize, PhantomData<T>);

pub(super) type BodyCursor = AnyCursor<Body>;
pub(super) type TitleCursor = AnyCursor<Title>;
pub(super) type GroupCursor = AnyCursor<Group>;
pub(super) type PropCursor = AnyCursor<Prop>;

pub(super) trait Type {}
Expand All @@ -17,13 +16,10 @@ pub(super) struct Body {}
#[derive(Clone, Copy, Debug)]
pub(super) struct Title {}
#[derive(Clone, Copy, Debug)]
pub(super) struct Group {}
#[derive(Clone, Copy, Debug)]
pub(super) struct Prop {}

impl Type for Body {}
impl Type for Title {}
impl Type for Group {}
impl Type for Prop {}

pub enum MoveResult {
Expand Down Expand Up @@ -107,7 +103,7 @@ impl BodyCursor {
self.0 += 1;
MoveResult::Others
}
_ => todo!(),
_ => todo!("unknown block {}", structure[self.0]),
}
}

Expand Down Expand Up @@ -149,10 +145,10 @@ impl TitleCursor {
}

/// 生成组光标。
pub fn take_group_on(&self, dtb: RefDtb, name: &str) -> (GroupCursor, usize, BodyCursor) {
pub fn take_group_on(&self, dtb: RefDtb, name: &str) -> (BodyCursor, usize, BodyCursor) {
let name_bytes = name.as_bytes();
let name_skip = align(name_bytes.len() + 1, BLOCK_LEN);
let group = AnyCursor::<Group>(self.0, PhantomData);
let group = AnyCursor::<Body>(self.0, PhantomData);

let mut body = AnyCursor::<Body>(self.0 + 1 + name_skip, PhantomData);
let mut len = 1;
Expand Down Expand Up @@ -194,57 +190,6 @@ impl TitleCursor {
}
}

impl GroupCursor {
/// 读取缓存的下一项偏移。
pub fn offset_on(&self, dtb: RefDtb) -> usize {
(dtb.borrow().structure[self.0].0 >> 8) as _
}

/// 利用缓存的名字长度取出名字。
pub fn name_on<'a>(&self, dtb: RefDtb<'a>) -> (&'a [u8], BodyCursor) {
let structure = &dtb.borrow().structure;
let len_name = (structure[self.0].0 & 0xff) as usize;
let bytes = structure[self.0 + 1].lead_slice(len_name);
(
bytes,
AnyCursor(self.0 + 1 + align(len_name + 1, BLOCK_LEN), PhantomData),
)
}

/// 初始化组反序列化。
pub fn init_on(&self, dtb: RefDtb, len_item: usize, len_name: usize) {
let mut body = AnyCursor::<Body>(self.0, PhantomData);
for _ in 0..len_item {
let current = body.0;
let len_total = dtb.borrow().structure[current + 1]
.lead_slice(u16::MAX as _)
.iter()
.enumerate()
.skip(len_name + 1)
.find(|(_, b)| **b == b'\0')
.map(|(i, _)| i)
.unwrap();
body.step_n(align(len_total, BLOCK_LEN));
body.skip_str_on(dtb);
body.escape_from(dtb);
let off_next = body.0 - current;
dtb.borrow_mut().structure[current].0 = (off_next << 8 | len_total) as _;
}
}

/// 组结构恢复原状。
pub fn drop_on(&self, dtb: RefDtb, len_item: usize) {
use StructureBlock as B;
let structure = &mut *dtb.borrow_mut().structure;
let mut i = self.0;
for _ in 0..len_item {
let offset = (structure[i].0 >> 8) as usize;
structure[i] = B::NODE_BEGIN;
i += offset;
}
}
}

impl PropCursor {
pub fn name_on<'a>(&self, dtb: RefDtb<'a>) -> (&'a str, BodyCursor) {
let dtb = dtb.borrow();
Expand Down
50 changes: 34 additions & 16 deletions src/de_mut/data.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use super::BodyCursor;
use super::{BodyCursor, Cursor};
use super::{DtError, PropCursor, RefDtb, RegConfig};

use core::marker::PhantomData;
use serde::{de, Deserialize};

#[derive(Clone, Copy, Debug)]
pub(super) enum ValueCursor {
Prop(PropCursor),
Prop(BodyCursor, PropCursor),
Body(BodyCursor),
}

#[derive(Clone)]
#[derive(Clone, Copy)]
pub(super) struct ValueDeserializer<'de> {
pub dtb: RefDtb<'de>,
pub reg: RegConfig,
pub body_cursor: BodyCursor,
pub cursor: ValueCursor,
}

Expand All @@ -39,8 +38,7 @@ impl<'de> Deserialize<'de> for ValueDeserializer<'_> {
D: de::Deserializer<'de>,
{
Ok(unsafe {
(*(core::ptr::addr_of!(deserializer) as *const _ as *const &ValueDeserializer))
.clone()
*(*(core::ptr::addr_of!(deserializer) as *const _ as *const &ValueDeserializer))
})
}
}
Expand Down Expand Up @@ -70,7 +68,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
where
V: de::Visitor<'de>,
{
if let ValueCursor::Prop(cursor) = self.cursor {
if let ValueCursor::Prop(_, cursor) = self.cursor {
let val = cursor.map_on(self.dtb, |data| {
if data.is_empty() {
true
Expand Down Expand Up @@ -129,7 +127,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
where
V: de::Visitor<'de>,
{
if let ValueCursor::Prop(cursor) = self.cursor {
if let ValueCursor::Prop(_, cursor) = self.cursor {
return visitor.visit_u32(cursor.map_u32_on(self.dtb)?);
}
unreachable!("node -> u32");
Expand Down Expand Up @@ -181,7 +179,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
where
V: de::Visitor<'de>,
{
if let ValueCursor::Prop(cursor) = self.cursor {
if let ValueCursor::Prop(_, cursor) = self.cursor {
let data = cursor.data_on(self.dtb);
return visitor.visit_borrowed_bytes(data);
}
Expand All @@ -200,7 +198,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
V: de::Visitor<'de>,
{
match self.cursor {
ValueCursor::Prop(cursor) => {
ValueCursor::Prop(_, cursor) => {
let data = cursor.data_on(self.dtb);
if data.is_empty() {
visitor.visit_none()
Expand Down Expand Up @@ -242,7 +240,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
return visitor.visit_newtype_struct(self);
}
match self.cursor {
ValueCursor::Prop(cursor) => match name {
ValueCursor::Prop(_, cursor) => match name {
"StrSeq" => {
let inner = super::str_seq::Inner {
dtb: self.dtb,
Expand Down Expand Up @@ -274,11 +272,31 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
}
}

fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
unreachable!("seq")
use super::{StructAccess, StructAccessType, Temp};
match self.move_on() {
Cursor::Title(c) => {
let (name, _) = c.split_on(self.dtb);
let cursor = match self.cursor {
ValueCursor::Body(cursor) => cursor,
_ => unreachable!(""),
};

let pre_len = name.as_bytes().iter().take_while(|b| **b != b'@').count();
let name_bytes = &name.as_bytes()[..pre_len];
let name = unsafe { core::str::from_utf8_unchecked(name_bytes) };

visitor.visit_seq(StructAccess {
access_type: StructAccessType::Seq(name),
temp: Temp::Node(cursor, cursor),
de: self,
})
}
_ => unreachable!("seq request on a none seq cursor"),
}
}

fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -308,7 +326,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
if let ValueCursor::Body(cursor) = self.cursor {
return visitor.visit_map(StructAccess {
access_type: StructAccessType::Map(false),
temp: Temp::Node(self.body_cursor, cursor),
temp: Temp::Node(cursor, cursor),
de: self,
});
};
Expand All @@ -328,7 +346,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {
if let ValueCursor::Body(cursor) = self.cursor {
return visitor.visit_map(StructAccess {
access_type: StructAccessType::Struct(fields),
temp: Temp::Node(self.body_cursor, cursor),
temp: Temp::Node(cursor, cursor),
de: self,
});
};
Expand Down Expand Up @@ -364,7 +382,7 @@ impl<'de> de::Deserializer<'de> for &mut ValueDeserializer<'de> {

impl ValueDeserializer<'_> {
#[inline]
pub fn move_next(&mut self) -> super::Cursor {
pub fn move_on(&mut self) -> super::Cursor {
if let ValueCursor::Body(ref mut cursor) = self.cursor {
return cursor.move_on(self.dtb);
};
Expand Down
Loading

0 comments on commit 6b98e91

Please sign in to comment.