@@ -146,7 +146,7 @@ pub enum TryLockError {
146
146
/// use std::fs::Dir;
147
147
///
148
148
/// fn main() -> std::io::Result<()> {
149
- /// let dir = Dir::new("/home/ foo")?;
149
+ /// let dir = Dir::new("foo")?;
150
150
/// let file = dir.open("bar.txt")?;
151
151
/// Ok(())
152
152
/// }
@@ -1426,23 +1426,197 @@ impl Seek for Arc<File> {
1426
1426
}
1427
1427
1428
1428
impl Dir {
1429
- /// Opens a file relative to this directory.
1429
+ /// Attempts to open a directory at `path` in read-only mode.
1430
+ ///
1431
+ /// See [`new_with`] for more options.
1432
+ ///
1433
+ /// # Errors
1434
+ ///
1435
+ /// This function will return an error in these (and other) situations:
1436
+ /// * The path doesn't exist
1437
+ /// * The path doesn't specify a directory
1438
+ /// * The process doesn't have permission to read the directory
1439
+ ///
1440
+ /// # Examples
1441
+ ///
1442
+ /// ```no_run
1443
+ /// use std::fs::Dir;
1444
+ ///
1445
+ /// fn main() -> std::io::Result<()> {
1446
+ /// let dir = Dir::new("foo")?;
1447
+ /// let mut f = dir.open("bar.txt")?;
1448
+ /// let mut data = vec![];
1449
+ /// f.read_to_end(&mut data)?;
1450
+ /// Ok(())
1451
+ /// }
1452
+ /// ```
1453
+ ///
1454
+ /// [`new_with`]: Dir::new_with
1455
+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1456
+ pub fn new < P : AsRef < Path > > ( path : P ) -> io:: Result < Self > {
1457
+ Ok ( Self { inner : fs_imp:: Dir :: new ( path) ? } )
1458
+ }
1459
+
1460
+ /// Attempts to open a directory at `path` with the options specified by `opts`.
1461
+ ///
1462
+ /// # Errors
1463
+ ///
1464
+ /// This function will return an error in these (and other) situations:
1465
+ /// * The path doesn't exist
1466
+ /// * The path doesn't specify a directory
1467
+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
1430
1468
///
1431
1469
/// # Examples
1470
+ ///
1432
1471
/// ```no_run
1433
1472
/// use std::fs::Dir;
1434
1473
///
1435
- /// let dir = Dir::new("foo")?;
1474
+ /// fn main() -> std::io::Result<()> {
1475
+ /// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
1476
+ /// let mut f = dir.remove_file("bar.txt")?;
1477
+ /// Ok(())
1478
+ /// }
1479
+ /// ```
1480
+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1481
+ pub fn new_with < P : AsRef < Path > > ( path : P , opts : & OpenOptions ) -> io:: Result < Self > {
1482
+ Ok ( Self { inner : fs_imp:: Dir :: new_with ( path, & opts. 0 ) ? } )
1483
+ }
1484
+
1485
+ /// Attempts to open a file relative to this directory.
1486
+ ///
1487
+ /// # Errors
1488
+ ///
1489
+ /// This function will return an error in these (and other) situations:
1490
+ /// * The path doesn't exist
1491
+ /// * The path doesn't specify a regular file
1492
+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
1493
+ ///
1494
+ /// # Examples
1495
+ ///
1496
+ /// ```no_run
1497
+ /// use std::fs::Dir;
1498
+ ///
1499
+ /// fn main() -> std::io::Result<()> {
1500
+ /// let dir = Dir::new("foo")?;
1501
+ /// let mut f = dir.open("bar.txt")?;
1502
+ /// let mut data = vec![];
1503
+ /// f.read_to_end(&mut data)?;
1504
+ /// Ok(())
1505
+ /// }
1436
1506
/// ```
1437
1507
#[ unstable( feature = "dirfd" , issue = "120426" ) ]
1438
1508
pub fn open < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
1439
1509
self . inner . open ( path) . map ( |f| File { inner : f } )
1440
1510
}
1441
- /// Opens a file relative to this directory with the specified options.
1511
+
1512
+ /// Attempts to open a file relative to this directory with the options specified by `opts`.
1513
+ ///
1514
+ /// # Errors
1515
+ ///
1516
+ /// This function will return an error in these (and other) situations:
1517
+ /// * The path doesn't exist
1518
+ /// * The path doesn't specify a regular file
1519
+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
1520
+ ///
1521
+ /// # Examples
1522
+ ///
1523
+ /// ```no_run
1524
+ /// use std::fs::Dir;
1525
+ ///
1526
+ /// fn main() -> std::io::Result<()> {
1527
+ /// let dir = Dir::new("foo")?;
1528
+ /// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1529
+ /// let mut data = vec![];
1530
+ /// f.read_to_end(&mut data)?;
1531
+ /// Ok(())
1532
+ /// }
1533
+ /// ```
1442
1534
#[ unstable( feature = "dirfd" , issue = "120426" ) ]
1443
1535
pub fn open_with < P : AsRef < Path > > ( & self , path : P , opts : & OpenOptions ) -> io:: Result < File > {
1444
1536
self . inner . open_with ( path, & opts. 0 ) . map ( |f| File { inner : f } )
1445
1537
}
1538
+
1539
+ /// Attempts to remove a file relative to this directory.
1540
+ ///
1541
+ /// # Errors
1542
+ ///
1543
+ /// This function will return an error in these (and other) situations:
1544
+ /// * The path doesn't exist
1545
+ /// * The path doesn't specify a regular file
1546
+ /// * The process doesn't have permission to delete the file.
1547
+ ///
1548
+ /// # Examples
1549
+ ///
1550
+ /// ```no_run
1551
+ /// use std::fs::Dir;
1552
+ ///
1553
+ /// fn main() -> std::io::Result<()> {
1554
+ /// let dir = Dir::new("foo")?;
1555
+ /// dir.remove_file("bar.txt")?;
1556
+ /// Ok(())
1557
+ /// }
1558
+ /// ```
1559
+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1560
+ pub fn remove_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1561
+ self . inner . remove_file ( path)
1562
+ }
1563
+
1564
+ /// Attempts to remove a directory relative to this directory.
1565
+ ///
1566
+ /// # Errors
1567
+ ///
1568
+ /// This function will return an error in these (and other) situations:
1569
+ /// * The path doesn't exist
1570
+ /// * The path doesn't specify a directory
1571
+ /// * The directory isn't empty
1572
+ /// * The process doesn't have permission to delete the directory.
1573
+ ///
1574
+ /// # Examples
1575
+ ///
1576
+ /// ```no_run
1577
+ /// use std::fs::Dir;
1578
+ ///
1579
+ /// fn main() -> std::io::Result<()> {
1580
+ /// let dir = Dir::new("foo")?;
1581
+ /// dir.remove_dir("baz")?;
1582
+ /// Ok(())
1583
+ /// }
1584
+ /// ```
1585
+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1586
+ pub fn remove_dir < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1587
+ self . inner . remove_dir ( path)
1588
+ }
1589
+
1590
+ /// Attempts to rename a file or directory relative to this directory to a new name, replacing
1591
+ /// the destination file if present.
1592
+ ///
1593
+ /// # Errors
1594
+ ///
1595
+ /// This function will return an error in these (and other) situations:
1596
+ /// * The `from` path doesn't exist
1597
+ /// * The `from` path doesn't specify a directory
1598
+ /// * `self` and `to_dir` are on different mount points
1599
+ ///
1600
+ /// # Examples
1601
+ ///
1602
+ /// ```no_run
1603
+ /// use std::fs::Dir;
1604
+ ///
1605
+ /// fn main() -> std::io::Result<()> {
1606
+ /// let dir = Dir::new("foo")?;
1607
+ /// dir.rename("bar.txt", &dir, "quux.txt")?;
1608
+ /// Ok(())
1609
+ /// }
1610
+ /// ```
1611
+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1612
+ pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > (
1613
+ & self ,
1614
+ from : P ,
1615
+ to_dir : & Self ,
1616
+ to : Q ,
1617
+ ) -> io:: Result < ( ) > {
1618
+ self . inner . rename ( from, & to_dir. inner , to)
1619
+ }
1446
1620
}
1447
1621
1448
1622
#[ unstable( feature = "dirfd" , issue = "120426" ) ]
0 commit comments