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

Port kill-buffer #1507

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

Conversation

simon-frankau
Copy link
Contributor

A somewhat literal translation of kill-buffer to Rust.

There's a lot of unsafe code, including plenty of pointer work on linked lists. Compared to e.g. accessors, I don't see a particularly pleasant way to wrap up the modifications to these structures to hide the pointer-handling away in reusable helper functions (at least, that doesn't feel too forced).

I think useful next steps would be to port the other (no longer static) functions called by kill-buffer over to Rust, and perhaps break apart kill_buffer into smaller functions rather than one giant do-everything function, but I'd appreciate thoughts on what we have so far before going further.

rust_src/src/buffers.rs Show resolved Hide resolved
rust_src/src/buffers.rs Outdated Show resolved Hide resolved
@agraven
Copy link
Collaborator

agraven commented Jan 22, 2020

As this PR hasn't seen activity for quite some time I've taken the liberty of continuing it

@simon-frankau
Copy link
Contributor Author

Oh, wow, thank you! This PR fell off my radar as I've been busy with work. I'd love to get back onto it, but I doubt that's realistic any time soon. If you can push it forwards, that'd be awesome. Sorry for leaving it dangling.

@agraven
Copy link
Collaborator

agraven commented Jan 22, 2020

That's quite all right! Everyone has obligations from time to time 🙂. And there's a solid amount of groundwork to build on, so finishing it won't be too difficult.

@agraven agraven changed the title [WIP] Port kill-buffer Port kill-buffer Jan 23, 2020
@agraven
Copy link
Collaborator

agraven commented Jan 23, 2020

This function is somewhat long, I've managed to stare myself blind reading it over and over. I've used the rust versions of functions where their C exports were previously used, and I've added some iterator types to make some iterations idiomatic.

There are a couple very simple C functions that are invoked that could potentially be ported as well, but I refrained from doing so to keep things from blowing too far out of scope.

rust_src/src/buffers.rs Outdated Show resolved Hide resolved
rust_src/src/buffers.rs Outdated Show resolved Hide resolved
// We do it at this stage so nothing terrible happens if they
// ask questions or their hooks get errors.
if b.base_buffer.is_null() && b.indirections > 0 {
for other in LispBufferIter::new() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the idea of this iterator. But maybe we can come up with a name for the iteration over the global buffer list instead of the Iterator automatically walking that list.

Copy link
Collaborator

@agraven agraven Jan 23, 2020

Choose a reason for hiding this comment

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

So something like renaming the constructor from new to something like all or global_list or from_global?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally the LispBufferIter constructor could be given a list of buffers and there would be a function or constructor/factory that would give the iterator over the global list.

rust_src/src/buffers.rs Outdated Show resolved Hide resolved
rust_src/src/buffers.rs Outdated Show resolved Hide resolved
src/buffer.c Outdated Show resolved Hide resolved
src/buffer.c Outdated Show resolved Hide resolved
@shaleh
Copy link
Collaborator

shaleh commented Jan 23, 2020

@agraven thanks for picking this up. This function is a great example of the meandering, old-school C code. When we can refactor all of these into idiomatic code it will be nice nice nice.

Improved readability of matching on function argument, added wrapper for
accessing minibuf_window, removed unnecessary parts of c code
Rename the LispBufferIter constructor for iterating all buffers to
all_buffers, and add a constructor for starting at an arbitrary point.

Reflow comments in function, correct grammar, and clarify where needed.

Reduce the area marked as unsafe in the area regarding resetting local
variables.
pub fn all_buffers() -> Self {
unsafe {
Self {
current: LispBufferRef::from_ptr(all_buffers as *mut c_void),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that the constructor iterating all buffers doesn't rely on the one for starting at a specified point, as I felt safer accounting for the possibility of all_buffers being null.

@agraven
Copy link
Collaborator

agraven commented Jan 30, 2020

Whoops, didn't realize rustfmt wasn't set up correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants