@@ -40,6 +40,12 @@ 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+ pub enum SpliceState {
45+ Ended ,
46+ Fallback ,
47+ }
48+
4349/// Less noisy wrapper around [`rustix::pipe::splice`].
4450///
4551/// Up to `len` bytes are moved from `source` to `target`. Returns the number
@@ -81,11 +87,11 @@ pub fn might_fuse(source: &impl AsFd) -> bool {
8187}
8288
8389/// splice all of source to dest
84- /// return true if we need read/write fallback
85- /// fails if one of in/output should be pipe
90+ /// return `SpliceState` indicating if we need read/write fallback
91+ /// fallback if one of in/output should be pipe
8692#[ inline]
8793#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
88- pub fn splice_unbounded < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < bool >
94+ pub fn splice_unbounded < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < SpliceState >
8995where
9096 R : Read + AsFd ,
9197 S : AsFd ,
@@ -99,19 +105,19 @@ where
99105 loop {
100106 match splice ( & source, & dest, MAX_ROOTLESS_PIPE_SIZE ) {
101107 Ok ( 1 ..) => { }
102- Ok ( 0 ) => return Ok ( false ) ,
103- Err ( _) => return Ok ( true ) ,
108+ Ok ( 0 ) => return Ok ( SpliceState :: Ended ) ,
109+ Err ( _) => return Ok ( SpliceState :: Fallback ) ,
104110 }
105111 }
106112}
107113
108114/// force-splice source to dest even both of them are not pipe
109- /// return true if we need read/write fallback
115+ /// return `SpliceState` indicating if we need read/write fallback
110116///
111117/// This should not be used if one of them are pipe to save resources
112118#[ inline]
113119#[ 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 >
120+ pub fn splice_unbounded_broker < R , S > ( source : & R , dest : & mut S ) -> std:: io:: Result < SpliceState >
115121where
116122 R : Read + AsFd ,
117123 S : AsFd ,
@@ -121,7 +127,7 @@ where
121127 . get_or_init ( || pipe :: < false > ( MAX_ROOTLESS_PIPE_SIZE ) . ok ( ) )
122128 . as_ref ( )
123129 else {
124- return Ok ( true ) ;
130+ return Ok ( SpliceState :: Fallback ) ;
125131 } ;
126132 // improve throughput
127133 // no need to increase pipe size of input fd since
@@ -131,7 +137,7 @@ where
131137
132138 loop {
133139 match splice ( & source, & pipe_wr, MAX_ROOTLESS_PIPE_SIZE ) {
134- Ok ( 0 ) => return Ok ( false ) ,
140+ Ok ( 0 ) => return Ok ( SpliceState :: Ended ) ,
135141 Ok ( n) => {
136142 if splice_exact ( & pipe_rd, dest, n) . is_err ( ) {
137143 // If the first splice manages to copy to the intermediate
@@ -143,10 +149,10 @@ where
143149 let mut drain = Vec :: with_capacity ( n) ;
144150 pipe_rd. take ( n as u64 ) . read_to_end ( & mut drain) ?;
145151 crate :: io:: RawWriter ( & dest) . write_all ( & drain) ?;
146- return Ok ( true ) ;
152+ return Ok ( SpliceState :: Fallback ) ;
147153 }
148154 }
149- Err ( _) => return Ok ( true ) ,
155+ Err ( _) => return Ok ( SpliceState :: Fallback ) ,
150156 }
151157 }
152158}
0 commit comments