Demilade Sonuga's blog

All posts

Refactoring VII

2023-05-05 · 12 min read

Finally, we have an event-handling scheme in place. We're almost done with the project. But before we get on with the rest, let's do some refactoring.

In this post, our refactoring is going to focus on code organization (sensibly grouping components), rather than readability (how easy it is to understand the code). To start, take a look at the blasterball/src directory:


Our project has grown considerably big. These files contain multiple components of the code that each serve specific purposes. There are many ways these files could be regrouped. We could separate out crates and then include them in blasterball/Cargo.toml as dependencies. We could group the files into separate modules that clearly define what they are used for and how they are related together. And so on. For this project, we're just going to group the files into separate modules. Why? Because I think more it's straightforward.

Do this yourself now.

The modules you came up with and the names you assigned to them will probably be different from mine because we don't think the exact same way. But, just for this project, we'll go with mine so that we'll be on the same page in future posts.

From the way I'm viewing this project directory, I think we can group modules like so:

  1. All Intel structures and interfaces into a module machine.
  2. Allocators and data structures involving heap allocation into alloc.
  3. Anything to do with displaying things on screen into display.
  4., which drives execution, stays on its own.
  5., too, stays on its own. There isn't anything to group it with (at least, in this scheme).
  6. stays on its own since there isn't anything to group it with either.

After the refactoring and adding the files to the new modules directories, the blasterball/src directory should look like this:


Remember to re-export the submodules in the new modules:

In alloc/

pub mod allocator;
pub mod boxed_fn;
pub mod boxed;
pub mod vec;

In display/

pub mod bitmap;
pub mod font;

In machine/

pub mod gdt;
pub mod interrupts;
pub mod keyboard;
pub mod pics;
pub mod port;
pub mod tss;
pub mod uefi;

Updating the imports in

pub mod port;
mod font;
use font::FONT;
mod boxed;
mod vec;
mod boxed_fn;
mod event_hook;
use event_hook::{EventInfo, EventKind};
mod allocator;
mod uefi;
use uefi::{SystemTable, Screen, PixelFormat, Pixel, pre_graphics_print_str, print_str, printint,
NO_OF_PIXELS_IN_A_ROW, BootServices, MemType};

mod bitmap;
use bitmap::{FileHeader, DIBHeader, ColorTable, Color, Bitmap, draw_bitmap, erase_bitmap};

// This needs to be imported so that the functions it defines may
// be used
use core::fmt::Write;

mod gdt;
use gdt::{Descriptor, GDT, SegmentRegister, SegmentSelector};

mod tss;
use tss::{TSS, load_tss};

mod interrupts;
use interrupts::{IDT, Entry, ServiceRoutine, ServiceRoutineWithErrCode, ServiceRoutineWithNoReturn};

mod game;

mod pics;
use pics::PICs;

mod keyboard;
use keyboard::Keyboard;

mod machine;
mod alloc;
mod display;
mod event_hook;
mod game;

use core::fmt::Write;
use machine::{
uefi::{SystemTable, BootServices, Screen, MemType, PixelFormat},
gdt::{GDT, Descriptor, SegmentSelector, SegmentRegister},
tss::{TSS, load_tss},
interrupts::{IDT, ServiceRoutine, Entry, ServiceRoutineWithErrCode, ServiceRoutineWithNoReturn}
use alloc::{
use event_hook::{EventKind, EventInfo};


use crate::uefi::{Screen, NO_OF_PIXELS_IN_A_ROW, NO_OF_PIXELS_IN_A_COLUMN};
use crate::bitmap::{draw_bitmap, Bitmap};

// NEW:
use crate::machine::uefi::{Screen, NO_OF_PIXELS_IN_A_ROW, NO_OF_PIXELS_IN_A_COLUMN};
use crate::display::bitmap::{draw_bitmap, Bitmap};


use crate::keyboard::KeyEvent;
// DELETED: use crate::vec::Vec;
use crate::alloc::vec::Vec; // NEW
use crate::boxed_fn::BoxedFn;
use crate::allocator::{Allocator, get_allocator};
mod tests {
use super::*;
use crate::event_hook::EventInfo;
// DELETED: use crate::vec;
use crate::alloc::vec;

// ...Others


// DELETED: use crate::FONT;
use crate::display::font::FONT; // NEW
use core::fmt::Write;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::ops::{Index, IndexMut};


use crate::event_hook::EventInfo;
// DELETED: use crate::boxed::Box;
use crate::alloc::boxed::Box; // NEW
use crate::allocator::Allocator;


use crate::{Screen, Pixel};

use crate::machine::uefi::{
}; // NEW
use core::ops::Index;

And that's it for now. There is definitely still room for improvement. You can do that yourself. In the next post, we'll be focusing on the code itself and code quality in our refactoring.

Take Away

For the full code up to this point, go to the repo

In The Next Post

We'll continue refactoring