feat: utils: add Then for bool type

This commit is contained in:
Paul Pan 2024-04-06 21:12:32 +08:00
parent 2aa88154b4
commit ced2b85b63
3 changed files with 26 additions and 2 deletions

View File

@ -1,3 +1,4 @@
pub mod extern_addr; pub mod extern_addr;
pub mod function_name; pub mod function_name;
pub mod size; pub mod size;
pub mod then;

23
kernel/src/utils/then.rs Normal file
View File

@ -0,0 +1,23 @@
pub trait Then {
fn and<T, E, F: FnOnce() -> Result<T, E>>(self, f: F, err: E) -> Result<T, E>;
fn some<T, E, F: FnOnce() -> T>(self, f: F, err: E) -> Result<T, E>;
}
impl Then for bool {
#[inline]
fn and<T, E, F: FnOnce() -> Result<T, E>>(self, f: F, err: E) -> Result<T, E> {
if self {
f()
} else {
Err(err)
}
}
fn some<T, E, F: FnOnce() -> T>(self, f: F, err: E) -> Result<T, E> {
if self {
Ok(f())
} else {
Err(err)
}
}
}

View File

@ -105,8 +105,8 @@ impl FreeList {
} }
pub fn reserve(&mut self, start: PhysAddr, size: usize) { pub fn reserve(&mut self, start: PhysAddr, size: usize) {
if let Some((region, _)) = self if let Some((region, _)) =
.alloc_node(|region| (region.start_addr() <= start).chain(|| region.fit(size, 1), ())) self.alloc_node(|region| (region.start_addr() <= start).and(|| region.fit(size, 1), ()))
{ {
/* layout /* layout
* region: | before | [start +: size] | after | * region: | before | [start +: size] | after |