@@ -211,6 +211,74 @@ pub struct DirBuilder {
211
211
recursive : bool ,
212
212
}
213
213
214
+ /// Read the entire contents of a file into a bytes vector.
215
+ ///
216
+ /// This is a convenience function for using [`File::open`] and [`read_to_end`]
217
+ /// with fewer imports and without an intermediate variable.
218
+ ///
219
+ /// [`File::open`]: struct.File.html#method.open
220
+ /// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
221
+ ///
222
+ /// # Errors
223
+ ///
224
+ /// This function will return an error if `path` does not already exist.
225
+ /// Other errors may also be returned according to [`OpenOptions::open`].
226
+ ///
227
+ /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
228
+ ///
229
+ /// It will also return an error if it encounters while reading an error
230
+ /// of a kind other than [`ErrorKind::Interrupted`].
231
+ ///
232
+ /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
233
+ ///
234
+ /// # Examples
235
+ ///
236
+ /// ```no_run
237
+ /// #![feature(fs_read_write)]
238
+ ///
239
+ /// use std::fs;
240
+ /// use std::net::SocketAddr;
241
+ ///
242
+ /// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
243
+ /// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
244
+ /// # Ok(())
245
+ /// # }
246
+ /// ```
247
+ #[ unstable( feature = "fs_read_write" , issue = /* FIXME */ "0" ) ]
248
+ pub fn read < P : AsRef < Path > > ( path : P ) -> io:: Result < Vec < u8 > > {
249
+ let mut bytes = Vec :: new ( ) ;
250
+ File :: open ( path) ?. read_to_end ( & mut bytes) ?;
251
+ Ok ( bytes)
252
+ }
253
+
254
+ /// Write a slice as the entire contents of a file.
255
+ ///
256
+ /// This function will create a file if it does not exist,
257
+ /// and will entirely replace its contents if it does.
258
+ ///
259
+ /// This is a convenience function for using [`File::create`] and [`write_all`]
260
+ /// with fewer imports.
261
+ ///
262
+ /// [`File::create`]: struct.File.html#method.create
263
+ /// [`write_all`]: ../io/trait.Write.html#method.write_all
264
+ ///
265
+ /// # Examples
266
+ ///
267
+ /// ```no_run
268
+ /// #![feature(fs_read_write)]
269
+ ///
270
+ /// use std::fs;
271
+ ///
272
+ /// # fn foo() -> std::io::Result<()> {
273
+ /// fs::write("foo.txt", b"Lorem ipsum")?;
274
+ /// # Ok(())
275
+ /// # }
276
+ /// ```
277
+ #[ unstable( feature = "fs_read_write" , issue = /* FIXME */ "0" ) ]
278
+ pub fn write < P : AsRef < Path > > ( path : P , contents : & [ u8 ] ) -> io:: Result < ( ) > {
279
+ File :: create ( path) ?. write_all ( contents)
280
+ }
281
+
214
282
impl File {
215
283
/// Attempts to open a file in read-only mode.
216
284
///
@@ -262,73 +330,6 @@ impl File {
262
330
OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
263
331
}
264
332
265
- /// Read the entire contents of a file into a bytes vector.
266
- ///
267
- /// This is a convenience function for using [`File::open`] and [`read_to_end`]
268
- /// with fewer imports and without an intermediate variable.
269
- ///
270
- /// [`File::open`]: struct.File.html#method.open
271
- /// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
272
- ///
273
- /// # Errors
274
- ///
275
- /// This function will return an error if `path` does not already exist.
276
- /// Other errors may also be returned according to [`OpenOptions::open`].
277
- ///
278
- /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
279
- ///
280
- /// It will also return an error if it encounters while reading an error
281
- /// of a kind other than [`ErrorKind::Interrupted`].
282
- ///
283
- /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
284
- ///
285
- /// # Examples
286
- ///
287
- /// ```no_run
288
- /// #![feature(file_read_write_contents)]
289
- ///
290
- /// use std::fs::File;
291
- ///
292
- /// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
293
- /// let foo = String::from_utf8(File::read_contents("foo.txt")?)?;
294
- /// # Ok(())
295
- /// # }
296
- /// ```
297
- #[ unstable( feature = "file_read_write_contents" , issue = /* FIXME */ "0" ) ]
298
- pub fn read_contents < P : AsRef < Path > > ( path : P ) -> io:: Result < Vec < u8 > > {
299
- let mut bytes = Vec :: new ( ) ;
300
- File :: open ( path) ?. read_to_end ( & mut bytes) ?;
301
- Ok ( bytes)
302
- }
303
-
304
- /// Write the give contents to a file.
305
- ///
306
- /// This function will create a file if it does not exist,
307
- /// and will entirely replace its contents if it does.
308
- ///
309
- /// This is a convenience function for using [`File::create`] and [`write_all`]
310
- /// with fewer imports.
311
- ///
312
- /// [`File::create`]: struct.File.html#method.create
313
- /// [`write_all`]: ../io/trait.Write.html#method.write_all
314
- ///
315
- /// # Examples
316
- ///
317
- /// ```no_run
318
- /// #![feature(file_read_write_contents)]
319
- ///
320
- /// use std::fs::File;
321
- ///
322
- /// # fn foo() -> std::io::Result<()> {
323
- /// File::write_contents("foo.txt", b"Lorem ipsum")?;
324
- /// # Ok(())
325
- /// # }
326
- /// ```
327
- #[ unstable( feature = "file_read_write_contents" , issue = /* FIXME */ "0" ) ]
328
- pub fn write_contents < P : AsRef < Path > > ( path : P , contents : & [ u8 ] ) -> io:: Result < ( ) > {
329
- File :: create ( path) ?. write_all ( contents)
330
- }
331
-
332
333
/// Attempts to sync all OS-internal metadata to disk.
333
334
///
334
335
/// This function will attempt to ensure that all in-core data reaches the
@@ -2989,14 +2990,14 @@ mod tests {
2989
2990
}
2990
2991
2991
2992
#[ test]
2992
- fn write_contents_then_read_contents ( ) {
2993
+ fn write_then_read ( ) {
2993
2994
let mut bytes = [ 0 ; 1024 ] ;
2994
2995
StdRng :: new ( ) . unwrap ( ) . fill_bytes ( & mut bytes) ;
2995
2996
2996
2997
let tmpdir = tmpdir ( ) ;
2997
2998
2998
- check ! ( File :: write_contents ( & tmpdir. join( "test" ) , & bytes) ) ;
2999
- let v = check ! ( File :: read_contents ( & tmpdir. join( "test" ) ) ) ;
2999
+ check ! ( fs :: write ( & tmpdir. join( "test" ) , & bytes) ) ;
3000
+ let v = check ! ( fs :: read ( & tmpdir. join( "test" ) ) ) ;
3000
3001
assert ! ( v == & bytes[ ..] ) ;
3001
3002
}
3002
3003
0 commit comments