Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize Read
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Feb 8, 2024
1 parent 2d464c2 commit 66b4348
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ impl<'a> Deserializer<StrRead<'a>> {
}

impl<'de, R: Read<'de>> Deserializer<R> {
fn next_char(&mut self) -> Result<Option<u8>> {
fn next_char(&mut self) -> Option<u8> {
self.read.next()
}

fn peek(&mut self) -> Result<Option<u8>> {
fn peek(&mut self) -> Option<u8> {
self.read.peek()
}

fn end(&mut self) -> Result<()> {
match self.peek()? {
match self.peek() {
Some(_) => Err(de::Error::custom("not over yet")),
None => Ok(()),
}
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
{
self.de.inner.clear();
loop {
match self.de.next_char()? {
match self.de.next_char() {
None => {
return Ok(None);
}
Expand All @@ -363,7 +363,7 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
{
self.de.inner.clear();
loop {
match self.de.next_char()? {
match self.de.next_char() {
Some(b'\n') | Some(b'\t') | Some(b'\r') | None => {
return seed.deserialize(&mut *self.de);
}
Expand Down
22 changes: 10 additions & 12 deletions src/read.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use core::str;

use crate::error::Result;

pub trait Read<'de> {
fn next(&mut self) -> Result<Option<u8>>;
fn peek(&mut self) -> Result<Option<u8>>;
fn next(&mut self) -> Option<u8>;
fn peek(&mut self) -> Option<u8>;
fn discard(&mut self);
fn byte_offset(&self) -> usize;
}
Expand All @@ -26,23 +24,23 @@ impl<'a> SliceRead<'a> {

impl<'a> Read<'a> for SliceRead<'a> {
#[inline]
fn next(&mut self) -> Result<Option<u8>> {
Ok(if self.index < self.slice.len() {
fn next(&mut self) -> Option<u8> {
if self.index < self.slice.len() {
let ch = self.slice[self.index];
self.index += 1;
Some(ch)
} else {
None
})
}
}

#[inline]
fn peek(&mut self) -> Result<Option<u8>> {
Ok(if self.index < self.slice.len() {
fn peek(&mut self) -> Option<u8> {
if self.index < self.slice.len() {
Some(self.slice[self.index])
} else {
None
})
}
}

#[inline]
Expand All @@ -65,12 +63,12 @@ impl<'a> StrRead<'a> {

impl<'a> Read<'a> for StrRead<'a> {
#[inline]
fn next(&mut self) -> Result<Option<u8>> {
fn next(&mut self) -> Option<u8> {
self.delegate.next()
}

#[inline]
fn peek(&mut self) -> Result<Option<u8>> {
fn peek(&mut self) -> Option<u8> {
self.delegate.peek()
}

Expand Down

0 comments on commit 66b4348

Please sign in to comment.