1use cap_primitives::fs::{reopen, OpenOptions};
2#[cfg(any(feature = "std", feature = "async_std"))]
3use io_lifetimes::AsFilelike;
4#[cfg(feature = "async_std")]
5use io_lifetimes::FromFilelike;
6use std::io;
7
8pub trait Reopen {
10 fn reopen(&self, options: &OpenOptions) -> io::Result<Self>
27 where
28 Self: Sized;
29}
30
31impl Reopen for std::fs::File {
32 #[inline]
33 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
34 reopen(self, options)
35 }
36}
37
38#[cfg(feature = "std")]
39impl Reopen for cap_std::fs::File {
40 #[inline]
41 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
42 let file = reopen(
43 &AsFilelike::as_filelike_view::<std::fs::File>(self),
44 options,
45 )?;
46 Ok(Self::from_std(file))
47 }
48}
49
50#[cfg(all(feature = "std", feature = "fs_utf8"))]
51impl Reopen for cap_std::fs_utf8::File {
52 #[inline]
53 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
54 let file = reopen(&self.as_filelike_view::<std::fs::File>(), options)?;
55 Ok(Self::from_std(file))
56 }
57}
58
59#[cfg(feature = "async_std")]
60impl Reopen for async_std::fs::File {
61 #[inline]
62 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
63 let file = reopen(&self.as_filelike_view::<std::fs::File>(), options)?;
64 Ok(async_std::fs::File::from_into_filelike(file))
65 }
66}
67
68#[cfg(feature = "async_std")]
69impl Reopen for cap_async_std::fs::File {
70 #[inline]
71 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
72 let file = reopen(&self.as_filelike_view::<std::fs::File>(), options)?;
73 let std = async_std::fs::File::from_into_filelike(file);
74 Ok(Self::from_std(std))
75 }
76}
77
78#[cfg(all(feature = "async_std", feature = "fs_utf8"))]
79impl Reopen for cap_async_std::fs_utf8::File {
80 #[inline]
81 fn reopen(&self, options: &OpenOptions) -> io::Result<Self> {
82 let file = reopen(&self.as_filelike_view::<std::fs::File>(), options)?;
83 let std = async_std::fs::File::from_into_filelike(file);
84 Ok(Self::from_std(std))
85 }
86}