Skip to content

Commit

Permalink
do not implement DerefMut
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Oct 30, 2024
1 parent 71b7593 commit 803ef27
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
31 changes: 15 additions & 16 deletions src/buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Primitive;
use std::ops::{Deref, DerefMut};
use std::ops::Deref;

/// Track the limits of data types used in a buffer.
#[derive(Clone, PartialEq, Debug, Default)]
Expand Down Expand Up @@ -87,39 +87,33 @@ impl Limits {
/// A buffer of arbitrary PDF content.
#[derive(Clone, PartialEq, Debug)]
pub struct Buf {
buf: Vec<u8>,
pub(crate) inner: Vec<u8>,
pub(crate) limits: Limits,
}

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

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

impl DerefMut for Buf {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.buf
&self.inner
}
}

impl Buf {
pub(crate) fn new() -> Self {
Self { buf: Vec::new(), limits: Limits::new() }
Self { inner: Vec::new(), limits: Limits::new() }
}

pub(crate) fn with_capacity(capacity: usize) -> Self {
Self {
buf: Vec::with_capacity(capacity),
inner: Vec::with_capacity(capacity),
limits: Limits::new(),
}
}

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

/// Return the limits of the buffer.
Expand Down Expand Up @@ -160,7 +154,7 @@ impl Buf {
#[inline(never)]
fn write_extreme(buf: &mut Buf, value: f32) {
use std::io::Write;
write!(buf, "{}", value).unwrap();
write!(buf.inner, "{}", value).unwrap();
}

write_extreme(self, value);
Expand All @@ -169,18 +163,18 @@ impl Buf {

#[inline]
pub(crate) fn extend_slice(&mut self, other: &[u8]) {
self.buf.extend(other);
self.inner.extend(other);
}

#[inline]
pub(crate) fn extend(&mut self, other: &Buf) {
self.limits.merge(&other.limits);
self.buf.extend(&other.buf);
self.inner.extend(&other.inner);
}

#[inline]
pub(crate) fn push(&mut self, b: u8) {
self.buf.push(b);
self.inner.push(b);
}

#[inline]
Expand Down Expand Up @@ -213,4 +207,9 @@ impl Buf {
self.push(octal((value >> 3) & 7));
self.push(octal(value & 7));
}

#[inline]
pub(crate) fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
}
2 changes: 1 addition & 1 deletion src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Content {
/// Return the raw constructed byte stream.
pub fn finish(mut self) -> Buf {
if self.buf.last() == Some(&b'\n') {
self.buf.pop();
self.buf.inner.pop();
}
self.buf
}
Expand Down
2 changes: 1 addition & 1 deletion src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ where
}

self.count = 0;
self.mappings.clear();
self.mappings.inner.clear();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl<'a> PostScriptOp<'a> {
buf.push(b' ');
}
if ops.len() == 1 {
buf.pop();
buf.inner.pop();
}
buf.push(b'}');
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Pdf {
///
/// _Default value_: \x80\x80\x80\x80
pub fn set_binary_marker(&mut self, marker: &[u8; 4]) {
self.chunk.buf[10..14].copy_from_slice(marker);
self.chunk.buf.inner[10..14].copy_from_slice(marker);
}

/// Set the PDF version.
Expand All @@ -252,10 +252,10 @@ impl Pdf {
/// _Default value_: 1.7.
pub fn set_version(&mut self, major: u8, minor: u8) {
if major < 10 {
self.chunk.buf[5] = b'0' + major;
self.chunk.buf.inner[5] = b'0' + major;
}
if minor < 10 {
self.chunk.buf[7] = b'0' + minor;
self.chunk.buf.inner[7] = b'0' + minor;
}
}

Expand Down Expand Up @@ -305,7 +305,7 @@ impl Pdf {
buf.push(b'\n');

if offsets.is_empty() {
write!(buf, "0000000000 65535 f\r\n").unwrap();
write!(buf.inner, "0000000000 65535 f\r\n").unwrap();
}

let mut written = 0;
Expand All @@ -330,11 +330,11 @@ impl Pdf {
}

let gen = if free_id == 0 { "65535" } else { "00000" };
write!(buf, "{:010} {} f\r\n", next % xref_len, gen).unwrap();
write!(buf.inner, "{:010} {} f\r\n", next % xref_len, gen).unwrap();
written += 1;
}

write!(buf, "{:010} 00000 n\r\n", offset).unwrap();
write!(buf.inner, "{:010} 00000 n\r\n", offset).unwrap();
written += 1;
}

Expand Down Expand Up @@ -362,7 +362,7 @@ impl Pdf {

// Write where the cross-reference table starts.
buf.extend_slice(b"\nstartxref\n");
write!(buf, "{}", xref_offset).unwrap();
write!(buf.inner, "{}", xref_offset).unwrap();

// Write the end of file marker.
buf.extend_slice(b"\n%%EOF");
Expand Down
16 changes: 8 additions & 8 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Primitive for Str<'_> {
// - Hex strings for anything non-ASCII.
if self.0.iter().all(|b| b.is_ascii()) {
buf.reserve(self.0.len());
buf.push(b'(');
buf.inner.push(b'(');

let mut balanced = None;
for &byte in self.0 {
Expand Down Expand Up @@ -403,17 +403,17 @@ impl Primitive for Date {
buf.extend_slice(b"(D:");

(|| {
write!(buf, "{:04}", self.year).unwrap();
write!(buf, "{:02}", self.month?).unwrap();
write!(buf, "{:02}", self.day?).unwrap();
write!(buf, "{:02}", self.hour?).unwrap();
write!(buf, "{:02}", self.minute?).unwrap();
write!(buf, "{:02}", self.second?).unwrap();
write!(buf.inner, "{:04}", self.year).unwrap();
write!(buf.inner, "{:02}", self.month?).unwrap();
write!(buf.inner, "{:02}", self.day?).unwrap();
write!(buf.inner, "{:02}", self.hour?).unwrap();
write!(buf.inner, "{:02}", self.minute?).unwrap();
write!(buf.inner, "{:02}", self.second?).unwrap();
let utc_offset_hour = self.utc_offset_hour?;
if utc_offset_hour == 0 && self.utc_offset_minute == 0 {
buf.push(b'Z');
} else {
write!(buf, "{:+03}'{:02}", utc_offset_hour, self.utc_offset_minute)
write!(buf.inner, "{:+03}'{:02}", utc_offset_hour, self.utc_offset_minute)
.unwrap();
}
Some(())
Expand Down

0 comments on commit 803ef27

Please sign in to comment.