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

Feature renderable filter #48

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

markzporter
Copy link

@markzporter markzporter commented Jan 3, 2025

Work in progress PR for improving control character handling in the rendered output.

Implements:
#44

@alexrutar
Copy link
Contributor

Just a general comment, you can use cargo fmt to avoid having to manually format things.

@alexrutar alexrutar marked this pull request as draft January 3, 2025 21:39
@alexrutar
Copy link
Contributor

If you want to be able to run example files, you also need to edit Cargo.toml

Comment on lines 9 to 11
fn list_control_characters() -> Vec<char> {
(0x00..=0x1F).map(|i| char::from_u32(i).unwrap()).collect()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to return a Vec here, you can return impl Iterator<Item = char> to the same effect:

fn control_chars() -> impl Iterator<Item = char> {
    (0x00..=0x1F).map(|i| char::from_u32(i).unwrap())
}

and the code should still compile. The reason is that for looks in rust are actually based on Iterators: the thing you are looping over is anything that implements the IntoIterator trait. The lazy version is better since it saves on an allocation.

Copy link
Contributor

Choose a reason for hiding this comment

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

The impl ... return type says "I am returning a concrete type, but I am just not telling you exactly what it is"; it's not actually a dynamic object. The compiler will replace your impl ... block with the actual type of the thing that you return.

Copy link
Contributor

@alexrutar alexrutar Jan 3, 2025

Choose a reason for hiding this comment

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

Even better, you can directly construct a Range<char>:

const ASCII_CONTROL: Range<char> = '\x00'..='\x1F';

and just use that instead (since Range<char> implements IntoIterator<Item = char>).

Copy link
Author

Choose a reason for hiding this comment

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

Right so the idea being that we are only storing one char in memory at a time OK makes sense. Will make this change thanks!

@markzporter markzporter changed the title WIP: Feature renderable filter Feature renderable filter Jan 3, 2025
@markzporter
Copy link
Author

If you want to be able to run example files, you also need to edit Cargo.toml

Interestingly, I am able to run the example without this. But will add it nonetheless

@alexrutar
Copy link
Contributor

If you want to be able to run example files, you also need to edit Cargo.toml

Interestingly, I am able to run the example without this. But will add it nonetheless

Interesting! In my setup I can't even get syntax highlighting without including it as an example explicitly because rust analyzer complains that it isn't included in the project.

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