refactor(phase112): Ring0 Service Registry コード改善

Phase 112 実装後のコード品質向上として4つの改善を実施

【改善1】NoFsApi エラーメッセージの定数化
- NOFS_ERROR_MSG 定数を定義して一元管理
- 5箇所の重複メッセージを1つの定数参照に統一
- タイポリスク低減・保守性向上

【改善2】Ring0Registry の build_with_fs() 抽出
- build_default() と build_no_fs() の重複(14行)を削除
- 新規ヘルパーメソッド build_with_fs(fs: Arc<dyn FsApi>) を追加
- build() メソッドを2行の match 式に簡潔化
- 将来の profile 追加時の拡張性向上

【改善3】NoFsApi テスト追加
- 7つの新規テストを追加(全 FsApi メソッドをカバー)
  - test_nofs_api_read_to_string
  - test_nofs_api_read
  - test_nofs_api_write_all
  - test_nofs_api_append_all
  - test_nofs_api_exists
  - test_nofs_api_metadata
  - test_nofs_api_canonicalize
- テストカバレッジ大幅向上

【改善4】unsafe dealloc ヘルパー化
- unsafe_dealloc(ptr, size) ヘルパー関数を追加
- 3箇所の unsafe dealloc 呼び出しを統一
- コード可読性向上・unsafe 領域最小化

【統計】
- 2ファイル修正(+77行, -40行)
- テスト: 19 passed(既存12 + 新規7)
- ビルド: SUCCESS

【効果】
- コード重複削減(14行削除)
- テストカバレッジ向上(NoFsApi 全メソッドテスト化)
- 保守性向上(定数一元管理)
- 可読性向上(build() メソッド簡潔化)

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-04 03:18:49 +09:00
parent 04f476d6b4
commit 0a49a9c897
2 changed files with 77 additions and 41 deletions

View File

@ -70,29 +70,18 @@ impl Ring0Registry {
/// Ring0Context を profile に応じて構築
pub fn build(profile: RuntimeProfile) -> Ring0Context {
match profile {
RuntimeProfile::Default => Self::build_default(),
RuntimeProfile::NoFs => Self::build_no_fs(),
RuntimeProfile::Default => Self::build_with_fs(Arc::new(StdFs)),
RuntimeProfile::NoFs => Self::build_with_fs(Arc::new(NoFsApi)),
}
}
fn build_default() -> Ring0Context {
fn build_with_fs(fs: Arc<dyn FsApi>) -> Ring0Context {
Ring0Context {
mem: Arc::new(StdMem::new()),
io: Arc::new(StdIo),
time: Arc::new(StdTime),
log: Arc::new(StdLog),
fs: Arc::new(StdFs),
thread: Arc::new(StdThread),
}
}
fn build_no_fs() -> Ring0Context {
Ring0Context {
mem: Arc::new(StdMem::new()),
io: Arc::new(StdIo),
time: Arc::new(StdTime),
log: Arc::new(StdLog),
fs: Arc::new(NoFsApi), // Phase 112: NoFs profile では FsApi を disabled に
fs,
thread: Arc::new(StdThread),
}
}
@ -133,6 +122,15 @@ pub fn get_global_ring0() -> Arc<Ring0Context> {
mod tests {
use super::*;
fn unsafe_dealloc(ptr: *mut u8, size: usize) {
unsafe {
std::alloc::dealloc(
ptr,
std::alloc::Layout::from_size_align_unchecked(size, 1),
)
}
}
#[test]
fn test_ring0_context_creation() {
let ring0 = default_ring0();
@ -173,12 +171,7 @@ mod tests {
ring0.mem.free(ptr);
// Clean up
unsafe {
std::alloc::dealloc(
ptr,
std::alloc::Layout::from_size_align_unchecked(512, 1),
)
};
unsafe_dealloc(ptr, 512);
}
// Phase 112: Ring0Registry tests
@ -217,4 +210,48 @@ mod tests {
ctx.log.info("Test from default_ring0()");
assert!(ctx.time.now().is_ok());
}
#[test]
fn test_nofs_api_read_to_string() {
let api = NoFsApi;
let result = api.read_to_string(std::path::Path::new("/tmp/test.txt"));
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("disabled"));
}
#[test]
fn test_nofs_api_read() {
let api = NoFsApi;
assert!(api.read(std::path::Path::new("/tmp/test.txt")).is_err());
}
#[test]
fn test_nofs_api_write_all() {
let api = NoFsApi;
assert!(api.write_all(std::path::Path::new("/tmp/test.txt"), b"data").is_err());
}
#[test]
fn test_nofs_api_append_all() {
let api = NoFsApi;
assert!(api.append_all(std::path::Path::new("/tmp/test.txt"), b"data").is_err());
}
#[test]
fn test_nofs_api_exists() {
let api = NoFsApi;
assert!(!api.exists(std::path::Path::new("/tmp/test.txt")));
}
#[test]
fn test_nofs_api_metadata() {
let api = NoFsApi;
assert!(api.metadata(std::path::Path::new("/tmp/test.txt")).is_err());
}
#[test]
fn test_nofs_api_canonicalize() {
let api = NoFsApi;
assert!(api.canonicalize(std::path::Path::new("/tmp/test.txt")).is_err());
}
}

View File

@ -232,31 +232,25 @@ impl ThreadApi for StdThread {
/// FileSystem 操作がすべて「無効」として機能する。
/// Phase 109 の NoFsFileIoFileIo traitと異なり、
/// Ring0 レベルの FsApi trait を実装する。
const NOFS_ERROR_MSG: &str = "FileSystem operations disabled in no-fs profile";
pub struct NoFsApi;
impl FsApi for NoFsApi {
fn read_to_string(&self, _path: &Path) -> Result<String, IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
fn read(&self, _path: &Path) -> Result<Vec<u8>, IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
fn write_all(&self, _path: &Path, _data: &[u8]) -> Result<(), IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
fn append_all(&self, _path: &Path, _data: &[u8]) -> Result<(), IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
fn exists(&self, _path: &Path) -> bool {
@ -264,15 +258,11 @@ impl FsApi for NoFsApi {
}
fn metadata(&self, _path: &Path) -> Result<FsMetadata, IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
fn canonicalize(&self, _path: &Path) -> Result<PathBuf, IoError> {
Err(IoError::Other(
"FileSystem operations disabled in no-fs profile".to_string()
))
Err(IoError::Other(NOFS_ERROR_MSG.to_string()))
}
}
@ -282,6 +272,15 @@ impl FsApi for NoFsApi {
mod stdmem_tests {
use super::*;
fn unsafe_dealloc(ptr: *mut u8, size: usize) {
unsafe {
std::alloc::dealloc(
ptr,
std::alloc::Layout::from_size_align_unchecked(size, 1),
)
}
}
#[test]
fn test_stdmem_alloc() {
let mem = StdMem::new();
@ -293,7 +292,7 @@ mod stdmem_tests {
assert_eq!(stats.current, 1024, "current should be 1024");
// Clean up
unsafe { std::alloc::dealloc(ptr, std::alloc::Layout::from_size_align_unchecked(1024, 1)) };
unsafe_dealloc(ptr, 1024);
}
#[test]
@ -314,7 +313,7 @@ mod stdmem_tests {
assert_eq!(stats.freed, 1, "freed count should be 1");
// Clean up (actual deallocation)
unsafe { std::alloc::dealloc(ptr, std::alloc::Layout::from_size_align_unchecked(512, 1)) };
unsafe_dealloc(ptr, 512);
}
#[test]