Skip to content

Commit 31facdb

Browse files
author
Tom Dohrmann
committed
implement ranged index for idt
1 parent 0930340 commit 31facdb

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/structures/idt.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use bitflags::bitflags;
2626
use core::fmt;
2727
use core::marker::PhantomData;
2828
use core::ops::Bound::{Excluded, Included, Unbounded};
29-
use core::ops::{Deref, Index, IndexMut, RangeBounds};
29+
use core::ops::{
30+
Bound, Deref, Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive,
31+
RangeTo, RangeToInclusive,
32+
};
3033
use volatile::Volatile;
3134

3235
use super::gdt::SegmentSelector;
@@ -558,6 +561,48 @@ impl IndexMut<u8> for InterruptDescriptorTable {
558561
}
559562
}
560563

564+
macro_rules! impl_index_for_idt {
565+
($ty:ty) => {
566+
impl Index<$ty> for InterruptDescriptorTable {
567+
type Output = [Entry<HandlerFunc>];
568+
569+
/// Returns the IDT entry with the specified index.
570+
///
571+
/// Panics if index is outside the IDT (i.e. greater than 255) or if the entry is an
572+
/// exception that pushes an error code (use the struct fields for accessing these entries).
573+
#[inline]
574+
fn index(&self, index: $ty) -> &Self::Output {
575+
self.slice(index)
576+
}
577+
}
578+
579+
impl IndexMut<$ty> for InterruptDescriptorTable {
580+
/// Returns a mutable reference to the IDT entry with the specified index.
581+
///
582+
/// Panics if the entry is an exception that pushes an error code (use the struct fields for accessing these entries).
583+
#[inline]
584+
fn index_mut(&mut self, index: $ty) -> &mut Self::Output {
585+
self.slice_mut(index)
586+
}
587+
}
588+
};
589+
}
590+
591+
// this list was stolen from the list of implementors in https://doc.rust-lang.org/core/ops/trait.RangeBounds.html
592+
impl_index_for_idt!((Bound<&u8>, Bound<&u8>));
593+
impl_index_for_idt!((Bound<u8>, Bound<u8>));
594+
impl_index_for_idt!(Range<&u8>);
595+
impl_index_for_idt!(Range<u8>);
596+
impl_index_for_idt!(RangeFrom<&u8>);
597+
impl_index_for_idt!(RangeFrom<u8>);
598+
impl_index_for_idt!(RangeInclusive<&u8>);
599+
impl_index_for_idt!(RangeInclusive<u8>);
600+
impl_index_for_idt!(RangeTo<u8>);
601+
impl_index_for_idt!(RangeTo<&u8>);
602+
impl_index_for_idt!(RangeToInclusive<&u8>);
603+
impl_index_for_idt!(RangeToInclusive<u8>);
604+
impl_index_for_idt!(RangeFull);
605+
561606
/// An Interrupt Descriptor Table entry.
562607
///
563608
/// The generic parameter is some [`InterruptFn`], depending on the interrupt vector.

0 commit comments

Comments
 (0)