Skip to content

Commit 1fd6096

Browse files
committed
Turn Primitive into a sealed trait
1 parent 6b7a034 commit 1fd6096

1 file changed

Lines changed: 49 additions & 18 deletions

File tree

src/object.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ use std::num::NonZeroI32;
55

66
use super::*;
77
use crate::chunk::Settings;
8+
use crate::object::sealed::Sealed;
89

9-
/// A primitive PDF object.
10-
pub trait Primitive {
11-
/// Whether the primitive object starts with one of the PDF delimiter characters.
12-
const STARTS_WITH_DELIMITER: bool;
10+
mod sealed {
11+
use crate::Buf;
12+
13+
pub trait Sealed {
14+
/// Whether the primitive object starts with one of the PDF delimiter characters.
15+
const STARTS_WITH_DELIMITER: bool;
1316

14-
/// Write the object into a buffer.
15-
fn write(self, buf: &mut Buf);
17+
/// Write the object into a buffer.
18+
fn write(self, buf: &mut Buf);
19+
}
1620
}
1721

18-
impl<T: Primitive> Primitive for &T
22+
/// A primitive PDF object.
23+
pub trait Primitive: Sealed {}
24+
25+
impl<T: Sealed> Sealed for &T
1926
where
2027
T: Copy,
2128
{
@@ -27,7 +34,9 @@ where
2734
}
2835
}
2936

30-
impl Primitive for bool {
37+
impl<T: Primitive> Primitive for &T where T: Copy {}
38+
39+
impl Sealed for bool {
3140
const STARTS_WITH_DELIMITER: bool = false;
3241

3342
#[inline]
@@ -40,7 +49,9 @@ impl Primitive for bool {
4049
}
4150
}
4251

43-
impl Primitive for i32 {
52+
impl Primitive for bool {}
53+
54+
impl Sealed for i32 {
4455
const STARTS_WITH_DELIMITER: bool = false;
4556

4657
#[inline]
@@ -49,7 +60,9 @@ impl Primitive for i32 {
4960
}
5061
}
5162

52-
impl Primitive for f32 {
63+
impl Primitive for i32 {}
64+
65+
impl Sealed for f32 {
5366
const STARTS_WITH_DELIMITER: bool = false;
5467

5568
#[inline]
@@ -58,6 +71,8 @@ impl Primitive for f32 {
5871
}
5972
}
6073

74+
impl Primitive for f32 {}
75+
6176
/// A string object (any byte sequence).
6277
///
6378
/// This is written as `(Thing)`.
@@ -80,7 +95,7 @@ impl Str<'_> {
8095
}
8196
}
8297

83-
impl Primitive for Str<'_> {
98+
impl Sealed for Str<'_> {
8499
const STARTS_WITH_DELIMITER: bool = true;
85100

86101
fn write(self, buf: &mut Buf) {
@@ -134,6 +149,8 @@ impl Primitive for Str<'_> {
134149
}
135150
}
136151

152+
impl Primitive for Str<'_> {}
153+
137154
/// A unicode text string object.
138155
///
139156
/// This is written as a [`Str`] containing either bare ASCII (if possible) or a
@@ -146,7 +163,7 @@ impl Primitive for Str<'_> {
146163
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
147164
pub struct TextStr<'a>(pub &'a str);
148165

149-
impl Primitive for TextStr<'_> {
166+
impl Sealed for TextStr<'_> {
150167
const STARTS_WITH_DELIMITER: bool = true;
151168

152169
fn write(self, buf: &mut Buf) {
@@ -166,6 +183,8 @@ impl Primitive for TextStr<'_> {
166183
}
167184
}
168185

186+
impl Primitive for TextStr<'_> {}
187+
169188
/// An identifier for the natural language in a section of a
170189
/// [`TextStrWithLang`].
171190
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
@@ -233,7 +252,7 @@ impl LanguageIdentifier {
233252
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
234253
pub struct TextStrWithLang<'a, 'b>(pub &'b [(LanguageIdentifier, &'a str)]);
235254

236-
impl<'a, 'b> Primitive for TextStrWithLang<'a, 'b> {
255+
impl<'a, 'b> Sealed for TextStrWithLang<'a, 'b> {
237256
const STARTS_WITH_DELIMITER: bool = true;
238257

239258
fn write(self, buf: &mut Buf) {
@@ -268,6 +287,8 @@ impl<'a, 'b> Primitive for TextStrWithLang<'a, 'b> {
268287
}
269288
}
270289

290+
impl Primitive for TextStrWithLang<'_, '_> {}
291+
271292
fn write_utf16be_text_str_header(buf: &mut Buf) {
272293
buf.push(b'<');
273294
buf.push_hex(254);
@@ -311,7 +332,7 @@ impl<'a, 'b> TextStrLike for TextStrWithLang<'a, 'b> {}
311332
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
312333
pub struct Name<'a>(pub &'a [u8]);
313334

314-
impl Primitive for Name<'_> {
335+
impl Sealed for Name<'_> {
315336
const STARTS_WITH_DELIMITER: bool = true;
316337

317338
fn write(self, buf: &mut Buf) {
@@ -333,6 +354,8 @@ impl Primitive for Name<'_> {
333354
}
334355
}
335356

357+
impl Primitive for Name<'_> {}
358+
336359
/// Regular characters are a PDF concept.
337360
fn is_regular_character(byte: u8) -> bool {
338361
!matches!(
@@ -365,7 +388,7 @@ pub(crate) fn is_delimiter_character(byte: u8) -> bool {
365388
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
366389
pub struct Null;
367390

368-
impl Primitive for Null {
391+
impl Sealed for Null {
369392
const STARTS_WITH_DELIMITER: bool = false;
370393

371394
#[inline]
@@ -374,6 +397,8 @@ impl Primitive for Null {
374397
}
375398
}
376399

400+
impl Primitive for Null {}
401+
377402
/// A reference to an indirect object.
378403
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
379404
pub struct Ref(NonZeroI32);
@@ -416,7 +441,7 @@ impl Ref {
416441
}
417442
}
418443

419-
impl Primitive for Ref {
444+
impl Sealed for Ref {
420445
const STARTS_WITH_DELIMITER: bool = false;
421446

422447
#[inline]
@@ -426,6 +451,8 @@ impl Primitive for Ref {
426451
}
427452
}
428453

454+
impl Primitive for Ref {}
455+
429456
/// A rectangle, specified by two opposite corners.
430457
#[derive(Debug, Copy, Clone, PartialEq)]
431458
pub struct Rect {
@@ -454,7 +481,7 @@ impl Rect {
454481
}
455482
}
456483

457-
impl Primitive for Rect {
484+
impl Sealed for Rect {
458485
const STARTS_WITH_DELIMITER: bool = true;
459486

460487
#[inline]
@@ -473,6 +500,8 @@ impl Primitive for Rect {
473500
}
474501
}
475502

503+
impl Primitive for Rect {}
504+
476505
/// A date, written as a text string.
477506
///
478507
/// A field is only respected if all superior fields are supplied. For example,
@@ -571,7 +600,7 @@ impl Date {
571600
}
572601
}
573602

574-
impl Primitive for Date {
603+
impl Sealed for Date {
575604
const STARTS_WITH_DELIMITER: bool = true;
576605

577606
fn write(self, buf: &mut Buf) {
@@ -602,6 +631,8 @@ impl Primitive for Date {
602631
}
603632
}
604633

634+
impl Primitive for Date {}
635+
605636
/// Writer for an arbitrary object.
606637
#[must_use = "not consuming this leaves the writer in an inconsistent state"]
607638
pub struct Obj<'a> {

0 commit comments

Comments
 (0)