Skip to content

Commit 4808744

Browse files
committed
refactor: simplify FileLike with downcast-rs
1 parent fd39ddc commit 4808744

File tree

12 files changed

+19
-57
lines changed

12 files changed

+19
-57
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bitmaps = { version = "3.2.1", default-features = false }
3737
bytemuck.workspace = true
3838
cfg-if.workspace = true
3939
chrono = { version = "0.4.41", default-features = false }
40+
downcast-rs = { version = "2.0", default-features = false, features = ["sync"] }
4041
event-listener.workspace = true
4142
flatten_objects = "0.2.4"
4243
gimli = { version = "*", default-features = false, optional = true }

api/src/file/epoll.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use alloc::{
1313
task::Wake,
1414
};
1515
use core::{
16-
any::Any,
1716
hash::{Hash, Hasher},
1817
sync::atomic::{AtomicBool, Ordering},
1918
task::{Context, Waker},
@@ -437,10 +436,6 @@ impl FileLike for Epoll {
437436
fn path(&self) -> Cow<'_, str> {
438437
"anon_inode:[eventpoll]".into()
439438
}
440-
441-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
442-
self
443-
}
444439
}
445440

446441
impl Pollable for Epoll {

api/src/file/event.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{borrow::Cow, sync::Arc};
22
use core::{
3-
any::Any,
43
sync::atomic::{AtomicBool, AtomicU64, Ordering},
54
task::Context,
65
};
@@ -105,10 +104,6 @@ impl FileLike for EventFd {
105104
fn path(&self) -> Cow<'_, str> {
106105
"anon_inode:[eventfd]".into()
107106
}
108-
109-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
110-
self
111-
}
112107
}
113108

114109
impl Pollable for EventFd {

api/src/file/fs.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{borrow::Cow, string::ToString, sync::Arc};
22
use core::{
3-
any::Any,
43
ffi::c_int,
54
hint::likely,
65
sync::atomic::{AtomicBool, Ordering},
@@ -56,7 +55,7 @@ pub fn resolve_at(dirfd: c_int, path: Option<&str>, flags: u32) -> AxResult<Reso
5655
return Err(AxError::NotFound);
5756
}
5857
let file_like = get_file_like(dirfd)?;
59-
let f = file_like.clone().into_any();
58+
let f = file_like.clone();
6059
Ok(if let Some(file) = f.downcast_ref::<File>() {
6160
ResolveAtResult::File(file.inner().backend()?.location().clone())
6261
} else if let Some(dir) = f.downcast_ref::<Directory>() {
@@ -152,10 +151,6 @@ impl FileLike for File {
152151
Ok(metadata_to_kstat(&self.inner().location().metadata()?))
153152
}
154153

155-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
156-
self
157-
}
158-
159154
fn ioctl(&self, cmd: u32, arg: usize) -> AxResult<usize> {
160155
self.inner().backend()?.location().ioctl(cmd, arg)
161156
}
@@ -177,9 +172,7 @@ impl FileLike for File {
177172
where
178173
Self: Sized + 'static,
179174
{
180-
let any = get_file_like(fd)?.into_any();
181-
182-
any.downcast::<Self>().map_err(|any| {
175+
get_file_like(fd)?.downcast_arc().map_err(|any| {
183176
if any.is::<Directory>() {
184177
AxError::IsADirectory
185178
} else {
@@ -235,14 +228,9 @@ impl FileLike for Directory {
235228
path_for(&self.inner)
236229
}
237230

238-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
239-
self
240-
}
241-
242231
fn from_fd(fd: c_int) -> AxResult<Arc<Self>> {
243232
get_file_like(fd)?
244-
.into_any()
245-
.downcast::<Self>()
233+
.downcast_arc()
246234
.map_err(|_| AxError::NotADirectory)
247235
}
248236
}

api/src/file/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ mod pipe;
77
pub mod signalfd;
88

99
use alloc::{borrow::Cow, sync::Arc};
10-
use core::{any::Any, ffi::c_int, time::Duration};
10+
use core::{ffi::c_int, time::Duration};
1111

1212
use axerrno::{AxError, AxResult};
1313
use axfs::{FS_CONTEXT, OpenOptions};
1414
use axfs_ng_vfs::DeviceId;
1515
use axio::prelude::*;
1616
use axpoll::Pollable;
1717
use axtask::current;
18+
use downcast_rs::{DowncastSync, impl_downcast};
1819
use flatten_objects::FlattenObjects;
1920
use linux_raw_sys::general::{RLIMIT_NOFILE, stat, statx, statx_timestamp};
2021
use spin::RwLock;
@@ -133,7 +134,7 @@ impl<T: Read + IoBuf> ReadBuf for T {}
133134
pub type IoSrc<'a> = dyn ReadBuf + 'a;
134135

135136
#[allow(dead_code)]
136-
pub trait FileLike: Pollable + Send + Sync {
137+
pub trait FileLike: Pollable + DowncastSync {
137138
fn read(&self, _dst: &mut IoDst) -> AxResult<usize> {
138139
Err(AxError::InvalidInput)
139140
}
@@ -146,8 +147,6 @@ pub trait FileLike: Pollable + Send + Sync {
146147
Ok(Kstat::default())
147148
}
148149

149-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
150-
151150
fn path(&self) -> Cow<'_, str>;
152151

153152
fn ioctl(&self, _cmd: u32, _arg: usize) -> AxResult<usize> {
@@ -167,8 +166,7 @@ pub trait FileLike: Pollable + Send + Sync {
167166
Self: Sized + 'static,
168167
{
169168
get_file_like(fd)?
170-
.into_any()
171-
.downcast::<Self>()
169+
.downcast_arc()
172170
.map_err(|_| AxError::InvalidInput)
173171
}
174172

@@ -179,6 +177,7 @@ pub trait FileLike: Pollable + Send + Sync {
179177
add_file_like(Arc::new(self), cloexec)
180178
}
181179
}
180+
impl_downcast!(sync FileLike);
182181

183182
#[derive(Clone)]
184183
pub struct FileDescriptor {

api/src/file/net.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ impl FileLike for Socket {
4040
})
4141
}
4242

43-
fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync> {
44-
self
45-
}
46-
4743
fn nonblocking(&self) -> bool {
4844
let mut result = false;
4945
self.get_option(GetSocketOption::NonBlocking(&mut result))
@@ -65,8 +61,7 @@ impl FileLike for Socket {
6561
Self: Sized + 'static,
6662
{
6763
get_file_like(fd)?
68-
.into_any()
69-
.downcast::<Self>()
64+
.downcast_arc()
7065
.map_err(|_| AxError::NotASocket)
7166
}
7267
}

api/src/file/pidfd.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ impl FileLike for PidFd {
3030
fn path(&self) -> Cow<'_, str> {
3131
"anon_inode:[pidfd]".into()
3232
}
33-
34-
fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync> {
35-
self
36-
}
3733
}
3834

3935
impl Pollable for PidFd {

api/src/file/pipe.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{borrow::Cow, format, sync::Arc};
22
use core::{
3-
any::Any,
43
mem,
54
sync::atomic::{AtomicBool, Ordering},
65
task::Context,
@@ -190,10 +189,6 @@ impl FileLike for Pipe {
190189
format!("pipe:[{}]", self as *const _ as usize).into()
191190
}
192191

193-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
194-
self
195-
}
196-
197192
fn set_nonblocking(&self, nonblocking: bool) -> AxResult {
198193
self.non_blocking.store(nonblocking, Ordering::Release);
199194
Ok(())

api/src/file/signalfd.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{borrow::Cow, sync::Arc};
22
use core::{
3-
any::Any,
43
mem,
54
sync::atomic::{AtomicBool, Ordering},
65
task::Context,
@@ -164,10 +163,6 @@ impl FileLike for Signalfd {
164163
fn path(&self) -> Cow<'_, str> {
165164
"anon_inode:[signalfd]".into()
166165
}
167-
168-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
169-
self
170-
}
171166
}
172167

173168
impl Pollable for Signalfd {

0 commit comments

Comments
 (0)