From e1e5f9f575c305a665ab8dd0472f4fd5c41aaf13 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Sun, 20 Apr 2025 23:14:03 +0800 Subject: [PATCH] Remove `transmute` in `Vm{Reader,Writer}` --- ostd/src/mm/io.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ostd/src/mm/io.rs b/ostd/src/mm/io.rs index d02a07ba..a28ba62a 100644 --- a/ostd/src/mm/io.rs +++ b/ostd/src/mm/io.rs @@ -548,10 +548,13 @@ impl<'a> VmReader<'a, Infallible> { /// Converts to a fallible reader. pub fn to_fallible(self) -> VmReader<'a, Fallible> { - // SAFETY: It is safe to transmute to a fallible reader since - // 1. the fallibility is a zero-sized marker type, - // 2. an infallible reader covers the capabilities of a fallible reader. - unsafe { core::mem::transmute(self) } + // It is safe to construct a fallible reader since an infallible reader covers the + // capabilities of a fallible reader. + VmReader { + cursor: self.cursor, + end: self.end, + phantom: PhantomData, + } } } @@ -799,10 +802,13 @@ impl<'a> VmWriter<'a, Infallible> { /// Converts to a fallible writer. pub fn to_fallible(self) -> VmWriter<'a, Fallible> { - // SAFETY: It is safe to transmute to a fallible writer since - // 1. the fallibility is a zero-sized marker type, - // 2. an infallible reader covers the capabilities of a fallible reader. - unsafe { core::mem::transmute(self) } + // It is safe to construct a fallible reader since an infallible reader covers the + // capabilities of a fallible reader. + VmWriter { + cursor: self.cursor, + end: self.end, + phantom: PhantomData, + } } }