mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: utils: add Then for bool type
This commit is contained in:
parent
2aa88154b4
commit
ced2b85b63
@ -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
23
kernel/src/utils/then.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 |
|
||||||
|
Loading…
Reference in New Issue
Block a user