Skip to content

Commit

Permalink
Apply some code review
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Nov 12, 2024
1 parent f95a19c commit e32816a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion benches/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn bench_content() -> Vec<u8> {
c.save_state();
c.set_flatness(10);
c.restore_state();
c.finish().to_bytes()
c.finish().into_bytes()
}

fn bench_new() -> Pdf {
Expand Down
69 changes: 35 additions & 34 deletions src/buf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::Primitive;

use std::ops::Deref;

/// Track the limits of data types used in a buffer.
/// Tracks the limits of data types used in a buffer.
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Limits {
int: i32,
Expand All @@ -18,30 +19,6 @@ impl Limits {
Self::default()
}

pub(crate) fn register_int(&mut self, val: i32) {
self.int = self.int.max(val.abs());
}

pub(crate) fn register_real(&mut self, val: f32) {
self.real = self.real.max(val.abs());
}

pub(crate) fn register_name_len(&mut self, len: usize) {
self.name_len = self.name_len.max(len);
}

pub(crate) fn register_str_len(&mut self, len: usize) {
self.str_len = self.str_len.max(len);
}

pub(crate) fn register_array_len(&mut self, len: usize) {
self.array_len = self.array_len.max(len);
}

pub(crate) fn register_dict_entries(&mut self, len: usize) {
self.dict_entries = self.dict_entries.max(len);
}

/// Get the absolute value of the largest positive/negative integer number.
pub fn int(&self) -> i32 {
self.int
Expand Down Expand Up @@ -72,6 +49,30 @@ impl Limits {
self.str_len
}

pub(crate) fn register_int(&mut self, val: i32) {
self.int = self.int.max(val.abs());
}

pub(crate) fn register_real(&mut self, val: f32) {
self.real = self.real.max(val.abs());
}

pub(crate) fn register_name_len(&mut self, len: usize) {
self.name_len = self.name_len.max(len);
}

pub(crate) fn register_str_len(&mut self, len: usize) {
self.str_len = self.str_len.max(len);
}

pub(crate) fn register_array_len(&mut self, len: usize) {
self.array_len = self.array_len.max(len);
}

pub(crate) fn register_dict_entries(&mut self, len: usize) {
self.dict_entries = self.dict_entries.max(len);
}

/// Merge two `Limits` with each other, taking the maximum
/// of each field from both.
pub fn merge(&mut self, other: &Limits) {
Expand All @@ -91,14 +92,6 @@ pub struct Buf {
pub(crate) limits: Limits,
}

impl Deref for Buf {
type Target = Vec<u8>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl Buf {
pub(crate) fn new() -> Self {
Self { inner: Vec::new(), limits: Limits::new() }
Expand All @@ -112,7 +105,7 @@ impl Buf {
}

/// Get the underlying bytes of the buffer.
pub fn to_bytes(self) -> Vec<u8> {
pub fn into_bytes(self) -> Vec<u8> {
self.inner
}

Expand Down Expand Up @@ -213,3 +206,11 @@ impl Buf {
self.inner.reserve(additional)
}
}

impl Deref for Buf {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.inner
}
}
6 changes: 3 additions & 3 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Chunk {

/// The bytes already written so far.
pub fn as_bytes(&self) -> &[u8] {
self.buf.as_slice()
self.buf.deref()
}

/// Return the limits of the chunk.
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Chunk {
/// file.
///
/// You can create the content bytes using a [`Content`] builder.
pub fn form_xobject<'a>(&'a mut self, id: Ref, content: &'a [u8]) -> FormXObject {
pub fn form_xobject<'a>(&'a mut self, id: Ref, content: &'a [u8]) -> FormXObject<'a> {
FormXObject::start(self.stream(id, content))
}

Expand Down Expand Up @@ -320,7 +320,7 @@ impl Chunk {
pub fn stream_shading<'a>(
&'a mut self,
id: Ref,
content: &'a Buf,
content: &'a [u8],
) -> StreamShading<'a> {
StreamShading::start(self.stream(id, content))
}
Expand Down
4 changes: 2 additions & 2 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ mod tests {
.restore_state();

assert_eq!(
content.finish().to_bytes(),
content.finish().into_bytes(),
b"q\n1 2 3 4 re\nf\n[7 2] 4 d\n/MyImage Do\n2 3.5 /MyPattern scn\nQ"
);
}
Expand All @@ -1677,7 +1677,7 @@ mod tests {
content.end_text();

assert_eq!(
content.finish().to_bytes(),
content.finish().into_bytes(),
b"/F1 12 Tf\nBT\n[] TJ\n[(AB) 2 (CD)] TJ\nET"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ mod tests {
];

assert_eq!(
&PostScriptOp::encode(&ops).to_bytes(),
&PostScriptOp::encode(&ops).into_bytes(),
b"{ 3.0 2.0 mul exch dup 0.0 ge { 1.0 add } {neg} ifelse add }"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl Pdf {

// Write the end of file marker.
buf.extend_slice(b"\n%%EOF");
buf.to_bytes()
buf.into_bytes()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/renumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ mod tests {

// Manually write an untidy object.
c.offsets.push((Ref::new(8), c.buf.len()));
// TODO: This won't update `limits` of `buf`.
c.buf.extend_slice(b"8 3 obj\n<</Fmt false/Niceness(4 0\nR-)");
c.buf.extend_slice(b"/beginobj/endobj%4 0 R\n");
c.buf
Expand All @@ -207,7 +206,7 @@ mod tests {
});

test!(
r.buf.to_bytes(),
r.buf.into_bytes(),
b"1 0 obj",
b"<<",
b" /Nested <<",
Expand Down

0 comments on commit e32816a

Please sign in to comment.