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

general code improvement #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

general code improvement #60

wants to merge 3 commits into from

Conversation

Vrtgs
Copy link

@Vrtgs Vrtgs commented Jul 26, 2024

With all due respect the code majorly sucks, but this is a little better, I mean... I can't do a full rewrite, just redid the low hanging fruit

@Vrtgs
Copy link
Author

Vrtgs commented Jul 26, 2024

ci failing isn't from me

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

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

Thanks for working on cleanup. Left some minor feedback. After that's addressed we can land this.

Please update your PR body to include a summary of your changes.

Doesn't need to mention everything, just the main ideas.

That summary will then be used for the final rebased commit message.

@@ -10,6 +10,7 @@ edition = "2018"

[dependencies]
thiserror = "1"
bytemuck = { version = "1.16.1", features = ["must_cast", "extern_crate_alloc"] }
Copy link
Owner

Choose a reason for hiding this comment

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

No need for the additional dependency.

We should also eventually remove thiserror (doesn't need to happen in this PR)

Copy link
Author

Choose a reason for hiding this comment

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

wait this should have been a test dep my bad, I was messing with utf-16 being 0 copy on big endian systems but then was like its just a bunch of added complexity I can still do it tho if you're fine with it

@@ -1,18 +0,0 @@
# Drag an Drop PSD Demo
Copy link
Owner

Choose a reason for hiding this comment

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

Why were these files removed from the example?

Comment on lines +22 to +26
impl Default for AppWrapper {
fn default() -> Self {
AppWrapper::new()
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

Can remove this if it is unused.

Copy link
Author

Choose a reason for hiding this comment

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

this is to stop a clippy lint, we can allow that if you so wish, or make AppWrapper not pub

@@ -147,7 +147,7 @@ impl Psd {
// Methods for working with layers
impl Psd {
/// Get all of the layers in the PSD
pub fn layers(&self) -> &Vec<PsdLayer> {
pub fn layers(&self) -> &[PsdLayer] {
Copy link
Owner

Choose a reason for hiding this comment

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

If there's a good reason to make these return &[T] we can consider that.

Otherwise, this is an unnecessary breaking change.
Can change these back to returning &Vec<T>.

Comment on lines +78 to +79
for alpha in rgba.iter_mut().skip(3).step_by(4) {
*alpha = 255;
Copy link
Owner

Choose a reason for hiding this comment

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

Less clear and potentially slower in unoptimized builds. So, doesn't seem like an improvement. Can revert.

@@ -20,7 +20,7 @@ impl SlicesImageResource {
&self.name
}

pub fn descriptors(&self) -> &Vec<DescriptorStructure> {
pub fn descriptors(&self) -> &[DescriptorStructure] {
Copy link
Owner

Choose a reason for hiding this comment

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

Can revert. If we want to introduce a breaking change it should be justified.

Copy link
Author

Choose a reason for hiding this comment

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

I mean, just bump up the semver version?????, and on most cases this works just fine, and this helps inference, as there are basically no methods on &Vec and if anyone is expecting &Vec they should use &[T]

self.groups.insert(group.id, group);
}

/// Get the group ID's in order (from bottom to top in a PSD file).
pub fn group_ids_in_order(&self) -> &Vec<u32> {
pub fn group_ids_in_order(&self) -> &[u32] {
Copy link
Owner

Choose a reason for hiding this comment

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

Can revert. If we want to introduce a breaking change it should be justified.

Copy link
Author

Choose a reason for hiding this comment

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

see comment above

Comment on lines +123 to +126
// since we are a cursor over a [u8]
// that means the total amount of bytes is less than isize::MAX
// therefore the biggest index is isize::MAX
// also meaning that it can be safely cast to a usize
Copy link
Owner

Choose a reason for hiding this comment

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

Let's clarify why this is true.

As in, why does a cursor over a [u8] have isize::MAX bytes?

Copy link
Author

Choose a reason for hiding this comment

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

from the standard library it states:

*The total size len * mem::size_of::<T>() of the slice must be no larger than isize::MAX, and adding that size to data must not “wrap around” the address space. See the safety documentation of pointer::offset.

and this also applies to all types, all allocations/types/abstract machine objects have to not wrap around the address space, so anything in memory must not be larger than isize::MAX bytes

pub fn seek(&mut self, pos: u64) {
self.cursor.set_position(pos);
pub fn seek(&mut self, pos: usize) {
// see Self::position for more info
Copy link
Owner

Choose a reason for hiding this comment

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

More info about what? Unless you're on a 128 bit architecture and your PSD has more than u64::MAX bytes this cast is always ok.

Copy link
Author

Choose a reason for hiding this comment

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

Well yeah I'm explaining why its fine, I'm saying if its in memory that for now (IIRC rust doesnt support 128 bit arch yet) means pos < u64::MAX so the cast is fine

}

pixels
bytemuck::allocation::cast_vec(vec![color; 8 * 8])
Copy link
Owner

Choose a reason for hiding this comment

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

Can remove the bytemuck dep. Not worth the extra dep for just this.

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