@@ -40,6 +40,13 @@ pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(File, File)
4040 Ok ( ( File :: from ( read) , File :: from ( write) ) )
4141}
4242
43+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
44+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
45+ pub enum SpliceState {
46+ Ended ,
47+ Fallback ,
48+ }
49+
4350/// Less noisy wrapper around [`rustix::pipe::splice`].
4451///
4552/// Up to `len` bytes are moved from `source` to `target`. Returns the number
@@ -81,11 +88,11 @@ pub fn might_fuse(source: &impl AsFd) -> bool {
8188}
8289
8390/// splice all of source to dest
84- /// return true if we need read/write fallback
91+ /// return SpliceState indicating if we need read/write fallback
8592/// fails if one of in/output should be pipe
8693#[ inline]
8794#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
88- pub fn splice_unbounded < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < bool >
95+ pub fn splice_unbounded < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < SpliceState >
8996where
9097 R : Read + AsFd ,
9198 S : AsFd ,
@@ -99,19 +106,19 @@ where
99106 loop {
100107 match splice ( & source, & dest, MAX_ROOTLESS_PIPE_SIZE ) {
101108 Ok ( 1 ..) => { }
102- Ok ( 0 ) => return Ok ( false ) ,
103- Err ( _) => return Ok ( true ) ,
109+ Ok ( 0 ) => return Ok ( SpliceState :: Ended ) ,
110+ Err ( _) => return Ok ( SpliceState :: Fallback ) ,
104111 }
105112 }
106113}
107114
108115/// force-splice source to dest even both of them are not pipe
109- /// return true if we need read/write fallback
116+ /// return SpliceState indicating if we need read/write fallback
110117///
111118/// This should not be used if one of them are pipe to save resources
112119#[ inline]
113120#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
114- pub fn splice_unbounded_broker < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < bool >
121+ pub fn splice_unbounded_broker < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < SpliceState >
115122where
116123 R : Read + AsFd ,
117124 S : AsFd ,
@@ -121,7 +128,7 @@ where
121128 . get_or_init ( || pipe :: < false > ( MAX_ROOTLESS_PIPE_SIZE ) . ok ( ) )
122129 . as_ref ( )
123130 else {
124- return Ok ( true ) ;
131+ return Ok ( SpliceState :: Fallback ) ;
125132 } ;
126133 // improve throughput
127134 // no need to increase pipe size of input fd since
@@ -131,7 +138,7 @@ where
131138
132139 loop {
133140 match splice ( & source, & pipe_wr, MAX_ROOTLESS_PIPE_SIZE ) {
134- Ok ( 0 ) => return Ok ( false ) ,
141+ Ok ( 0 ) => return Ok ( SpliceState :: Ended ) ,
135142 Ok ( n) => {
136143 if splice_exact ( & pipe_rd, dest, n) . is_err ( ) {
137144 // If the first splice manages to copy to the intermediate
@@ -143,10 +150,10 @@ where
143150 let mut drain = Vec :: with_capacity ( n) ;
144151 pipe_rd. take ( n as u64 ) . read_to_end ( & mut drain) ?;
145152 crate :: io:: RawWriter ( & dest) . write_all ( & drain) ?;
146- return Ok ( true ) ;
153+ return Ok ( SpliceState :: Fallback ) ;
147154 }
148155 }
149- Err ( _) => return Ok ( true ) ,
156+ Err ( _) => return Ok ( SpliceState :: Fallback ) ,
150157 }
151158 }
152159}
0 commit comments