use core::fmt;
use crate::CpuId;
use apic::ApicId;
impl From<ApicId> for CpuId {
fn from(apic_id: ApicId) -> Self {
CpuId(apic_id.value())
}
}
impl From<CpuId> for ApicId {
fn from(cpu_id: CpuId) -> Self {
ApicId::try_from(cpu_id.value()).expect("An invalid CpuId was encountered")
}
}
impl TryFrom<u32> for CpuId {
type Error = u32;
fn try_from(raw_cpu_id: u32) -> Result<Self, Self::Error> {
ApicId::try_from(raw_cpu_id)
.map(Into::into)
}
}
pub fn cpus() -> impl Iterator<Item = CpuId> {
apic::get_lapics().iter().map(|(apic_id, _)| (*apic_id).into())
}
pub fn cpu_count() -> u32 {
apic::cpu_count()
}
pub fn bootstrap_cpu() -> Option<CpuId> {
apic::bootstrap_cpu().map(Into::into)
}
pub fn is_bootstrap_cpu() -> bool {
apic::is_bootstrap_cpu()
}
pub fn current_cpu() -> CpuId {
apic::current_cpu().into()
}
#[derive(Copy, Clone)]
#[repr(align(8))]
pub struct OptionalCpuId(Option<CpuId>);
impl From<Option<CpuId>> for OptionalCpuId {
fn from(opt: Option<CpuId>) -> Self {
Self(opt)
}
}
impl From<OptionalCpuId> for Option<CpuId> {
fn from(val: OptionalCpuId) -> Self {
val.0
}
}
impl fmt::Debug for OptionalCpuId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}