foyer_storage/io/engine/
noop.rs

1// Copyright 2026 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use foyer_common::error::Result;
18use futures_core::future::BoxFuture;
19use futures_util::FutureExt;
20
21use crate::io::{
22    bytes::{IoB, IoBuf, IoBufMut},
23    device::Partition,
24    engine::{IoEngine, IoEngineBuildContext, IoEngineConfig, IoHandle},
25};
26/// Config for a no-operation mock I/O engine.
27#[derive(Debug, Default)]
28pub struct NoopIoEngineConfig;
29
30impl IoEngineConfig for NoopIoEngineConfig {
31    fn build(self: Box<Self>, _: IoEngineBuildContext) -> BoxFuture<'static, Result<Arc<dyn IoEngine>>> {
32        async move { Ok(Arc::new(NoopIoEngine) as Arc<dyn IoEngine>) }.boxed()
33    }
34}
35
36/// A mock I/O engine that does nothing.
37#[derive(Debug)]
38pub struct NoopIoEngine;
39
40impl IoEngine for NoopIoEngine {
41    fn read(&self, buf: Box<dyn IoBufMut>, _: &dyn Partition, _: u64) -> IoHandle {
42        async move {
43            let buf: Box<dyn IoB> = buf.into_iob();
44            (buf, Ok(()))
45        }
46        .boxed()
47        .into()
48    }
49
50    fn write(&self, buf: Box<dyn IoBuf>, _: &dyn Partition, _: u64) -> super::IoHandle {
51        async move {
52            let buf: Box<dyn IoB> = buf.into_iob();
53            (buf, Ok(()))
54        }
55        .boxed()
56        .into()
57    }
58}