Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genericize Content to allow AsRef<[Operation]> #111

Merged
merged 1 commit into from
Aug 5, 2020

Conversation

z33ky
Copy link
Contributor

@z33ky z33ky commented Aug 4, 2020

This was just Vec<Operation> before, which may be the most common use-case and is also required for parsing, since allocation is necessary.
When creating a document allocating a Vec per Content may not be necessary though, so for that use-case allowing any AsRef<[Operation]>, like just a slice, is convenient.

Example:

let content = Content {
	operations: /* note the missing vec!*/[
		Operation::new("BT", vec![]),
		Operation::new("Tf", vec!["F1".into(), 48.into()]),
		Operation::new("Td", vec![100.into(), 600.into()]),
		Operation::new("Tj", vec![Object::string_literal("Hello World!")]),
		Operation::new("ET", vec![]),
	],
};

The operands for Operation still require Vec. If the Operation struct were to be replaced with an enum as suggested in #109 (with an additional Custom { operation: AsRef<str>, operands: AsRef<[Object]> }) that would become unnecessary though. If you like the idea I'd be happy to make a PR for that as well.

This was just Vec<Operation> before, which may be the most common
use-case and is also required for parsing, since allocation is
necessary.
When creating a document allocating a Vec per Content may not be
necessary though, so for that use-case allowing any AsRef<[Operation]>,
like just a slice, is convenient.
@@ -20,15 +20,15 @@ impl Operation {
}

#[derive(Debug, Clone)]
pub struct Content {
pub operations: Vec<Operation>,
pub struct Content<Operations: AsRef<[Operation]> = Vec<Operation>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if having this default is desirable, but that should avoid downstream breakages.
If not now, then perhaps in a future version when a breaking change is added anyways, this can be removed if deemed inappropriate.

@J-F-Liu J-F-Liu merged commit 4461bff into J-F-Liu:master Aug 5, 2020
@z33ky z33ky deleted the asref-contents branch August 6, 2020 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants