Skip to content

Commit ebb085c

Browse files
authored
Add documentation about symlink's argument order. (#232)
The order of the arguments to the `symlink` function could reasonably go either of two different ways. And it appears cap-std picked one way and the openat crate [picked the other way]. Both ways have a reason why they make sense, and both ways also have the potential for user surprise. cap-std's overarching reason for its choice is to follow std as closely as possible. std's `symlink`, following POSIX `symlink` and `ln -s`, puts the link name second. So a user converting `std` code to `cap-std` code would just need to add a `Dir` as the `self` argument, without needing to reorder anything. I think that still makes sense, but I'm open to feedback here. One thing that makes sense no matter which way is chosen is to document it, so this patch adds documentation comments. And, it renames the arguments from `src` and `dst` to `original` and `link` to match the naming used in the `symlink` function in std. [picked the other way]: https://docs.rs/openat/0.1.21/openat/struct.Dir.html#method.symlink
1 parent 70e85f8 commit ebb085c

File tree

4 files changed

+185
-53
lines changed

4 files changed

+185
-53
lines changed

cap-async-std/src/fs/dir.rs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -535,28 +535,52 @@ impl Dir {
535535

536536
/// Creates a new symbolic link on a filesystem.
537537
///
538+
/// The `original` argument provides the target of the symlink. The `link`
539+
/// argument provides the name of the created symlink.
540+
///
541+
/// Despite the argument ordering, `original` is not resolved relative to
542+
/// `self` here. `link` is resolved relative to `self`, and `original` is
543+
/// not resolved within this function.
544+
///
545+
/// The `link` path is resolved when the symlink is dereferenced, relative
546+
/// to the directory that contains it.
547+
///
538548
/// This corresponds to [`async_std::os::unix::fs::symlink`], but only
539549
/// accesses paths relative to `self`.
540550
///
541551
/// [`async_std::os::unix::fs::symlink`]: https://docs.rs/async-std/latest/async_std/os/unix/fs/fn.symlink.html
542552
#[cfg(not(windows))]
543553
#[inline]
544-
pub async fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, dst: Q) -> io::Result<()> {
545-
let src = src.as_ref().to_path_buf();
546-
let dst = dst.as_ref().to_path_buf();
554+
pub async fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(
555+
&self,
556+
original: P,
557+
link: Q,
558+
) -> io::Result<()> {
559+
let original = original.as_ref().to_path_buf();
560+
let link = link.as_ref().to_path_buf();
547561
let clone = self.clone();
548562
spawn_blocking(move || {
549563
symlink(
550-
src.as_ref(),
564+
original.as_ref(),
551565
&clone.as_filelike_view::<std::fs::File>(),
552-
dst.as_ref(),
566+
link.as_ref(),
553567
)
554568
})
555569
.await
556570
}
557571

558572
/// Creates a new file symbolic link on a filesystem.
559573
///
574+
/// The `original` argument provides the target of the symlink. The `link`
575+
/// argument provides the name of the created symlink.
576+
///
577+
/// Despite the argument ordering, `original` is not resolved relative to
578+
/// `self` here. `link` is resolved relative to `self`, and `original` is
579+
/// not resolved within this function.
580+
///
581+
/// The `link` path is resolved when the symlink is dereferenced, relative
582+
/// to the directory that contains it.
583+
///
560584
/// This corresponds to [`async_std::os::windows::fs::symlink_file`], but
561585
/// only accesses paths relative to `self`.
562586
///
@@ -565,24 +589,34 @@ impl Dir {
565589
#[inline]
566590
pub async fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(
567591
&self,
568-
src: P,
569-
dst: Q,
592+
original: P,
593+
link: Q,
570594
) -> io::Result<()> {
571-
let src = src.as_ref().to_path_buf();
572-
let dst = dst.as_ref().to_path_buf();
595+
let original = original.as_ref().to_path_buf();
596+
let link = link.as_ref().to_path_buf();
573597
let clone = self.clone();
574598
spawn_blocking(move || {
575599
symlink_file(
576-
src.as_ref(),
600+
original.as_ref(),
577601
&clone.as_filelike_view::<std::fs::File>(),
578-
dst.as_ref(),
602+
link.as_ref(),
579603
)
580604
})
581605
.await
582606
}
583607

584608
/// Creates a new directory symlink on a filesystem.
585609
///
610+
/// The `original` argument provides the target of the symlink. The `link`
611+
/// argument provides the name of the created symlink.
612+
///
613+
/// Despite the argument ordering, `original` is not resolved relative to
614+
/// `self` here. `link` is resolved relative to `self`, and `original` is
615+
/// not resolved within this function.
616+
///
617+
/// The `link` path is resolved when the symlink is dereferenced, relative
618+
/// to the directory that contains it.
619+
///
586620
/// This corresponds to [`async_std::os::windows::fs::symlink_dir`], but
587621
/// only accesses paths relative to `self`.
588622
///
@@ -591,17 +625,17 @@ impl Dir {
591625
#[inline]
592626
pub async fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(
593627
&self,
594-
src: P,
595-
dst: Q,
628+
original: P,
629+
link: Q,
596630
) -> io::Result<()> {
597-
let src = src.as_ref().to_path_buf();
598-
let dst = dst.as_ref().to_path_buf();
631+
let original = original.as_ref().to_path_buf();
632+
let link = link.as_ref().to_path_buf();
599633
let clone = self.clone();
600634
spawn_blocking(move || {
601635
symlink_dir(
602-
src.as_ref(),
636+
original.as_ref(),
603637
&clone.as_filelike_view::<std::fs::File>(),
604-
dst.as_ref(),
638+
link.as_ref(),
605639
)
606640
})
607641
.await

cap-async-std/src/fs_utf8/dir.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,16 @@ impl Dir {
354354

355355
/// Creates a new symbolic link on a filesystem.
356356
///
357+
/// The `original` argument provides the target of the symlink. The `link`
358+
/// argument provides the name of the created symlink.
359+
///
360+
/// Despite the argument ordering, `original` is not resolved relative to
361+
/// `self` here. `link` is resolved relative to `self`, and `original` is
362+
/// not resolved within this function.
363+
///
364+
/// The `link` path is resolved when the symlink is dereferenced, relative
365+
/// to the directory that contains it.
366+
///
357367
/// This corresponds to [`async_std::os::unix::fs::symlink`], but only
358368
/// accesses paths relative to `self`.
359369
///
@@ -362,16 +372,26 @@ impl Dir {
362372
#[inline]
363373
pub async fn symlink<P: AsRef<Utf8Path>, Q: AsRef<Utf8Path>>(
364374
&self,
365-
src: P,
366-
dst: Q,
375+
original: P,
376+
link: Q,
367377
) -> io::Result<()> {
368-
let src = from_utf8(src)?;
369-
let dst = from_utf8(dst)?;
370-
self.cap_std.symlink(src, dst).await
378+
let original = from_utf8(original)?;
379+
let link = from_utf8(link)?;
380+
self.cap_std.symlink(original, link).await
371381
}
372382

373383
/// Creates a new file symbolic link on a filesystem.
374384
///
385+
/// The `original` argument provides the target of the symlink. The `link`
386+
/// argument provides the name of the created symlink.
387+
///
388+
/// Despite the argument ordering, `original` is not resolved relative to
389+
/// `self` here. `link` is resolved relative to `self`, and `original` is
390+
/// not resolved within this function.
391+
///
392+
/// The `link` path is resolved when the symlink is dereferenced, relative
393+
/// to the directory that contains it.
394+
///
375395
/// This corresponds to [`async_std::os::windows::fs::symlink_file`], but
376396
/// only accesses paths relative to `self`.
377397
///
@@ -380,16 +400,26 @@ impl Dir {
380400
#[inline]
381401
pub async fn symlink_file<P: AsRef<Utf8Path>, Q: AsRef<Utf8Path>>(
382402
&self,
383-
src: P,
384-
dst: Q,
403+
original: P,
404+
link: Q,
385405
) -> io::Result<()> {
386-
let src = from_utf8(src)?;
387-
let dst = from_utf8(dst)?;
388-
self.cap_std.symlink_file(src, dst).await
406+
let original = from_utf8(original)?;
407+
let link = from_utf8(link)?;
408+
self.cap_std.symlink_file(original, link).await
389409
}
390410

391411
/// Creates a new directory symlink on a filesystem.
392412
///
413+
/// The `original` argument provides the target of the symlink. The `link`
414+
/// argument provides the name of the created symlink.
415+
///
416+
/// Despite the argument ordering, `original` is not resolved relative to
417+
/// `self` here. `link` is resolved relative to `self`, and `original` is
418+
/// not resolved within this function.
419+
///
420+
/// The `link` path is resolved when the symlink is dereferenced, relative
421+
/// to the directory that contains it.
422+
///
393423
/// This corresponds to [`async_std::os::windows::fs::symlink_dir`], but
394424
/// only accesses paths relative to `self`.
395425
///
@@ -398,12 +428,12 @@ impl Dir {
398428
#[inline]
399429
pub async fn symlink_dir<P: AsRef<Utf8Path>, Q: AsRef<Utf8Path>>(
400430
&self,
401-
src: P,
402-
dst: Q,
431+
original: P,
432+
link: Q,
403433
) -> io::Result<()> {
404-
let src = from_utf8(src)?;
405-
let dst = from_utf8(dst)?;
406-
self.cap_std.symlink_dir(src, dst).await
434+
let original = from_utf8(original)?;
435+
let link = from_utf8(link)?;
436+
self.cap_std.symlink_dir(original, link).await
407437
}
408438

409439
/// Creates a new `UnixListener` bound to the specified socket.

cap-std/src/fs/dir.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,38 +395,76 @@ impl Dir {
395395

396396
/// Creates a new symbolic link on a filesystem.
397397
///
398+
/// The `original` argument provides the target of the symlink. The `link`
399+
/// argument provides the name of the created symlink.
400+
///
401+
/// Despite the argument ordering, `original` is not resolved relative to
402+
/// `self` here. `link` is resolved relative to `self`, and `original` is
403+
/// not resolved within this function.
404+
///
405+
/// The `link` path is resolved when the symlink is dereferenced, relative
406+
/// to the directory that contains it.
407+
///
398408
/// This corresponds to [`std::os::unix::fs::symlink`], but only accesses
399409
/// paths relative to `self`.
400410
///
401411
/// [`std::os::unix::fs::symlink`]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
402412
#[cfg(not(windows))]
403413
#[inline]
404-
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, dst: Q) -> io::Result<()> {
405-
symlink(src.as_ref(), &self.std_file, dst.as_ref())
414+
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, original: P, link: Q) -> io::Result<()> {
415+
symlink(original.as_ref(), &self.std_file, link.as_ref())
406416
}
407417

408418
/// Creates a new file symbolic link on a filesystem.
409419
///
420+
/// The `original` argument provides the target of the symlink. The `link`
421+
/// argument provides the name of the created symlink.
422+
///
423+
/// Despite the argument ordering, `original` is not resolved relative to
424+
/// `self` here. `link` is resolved relative to `self`, and `original` is
425+
/// not resolved within this function.
426+
///
427+
/// The `link` path is resolved when the symlink is dereferenced, relative
428+
/// to the directory that contains it.
429+
///
410430
/// This corresponds to [`std::os::windows::fs::symlink_file`], but only
411431
/// accesses paths relative to `self`.
412432
///
413433
/// [`std::os::windows::fs::symlink_file`]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html
414434
#[cfg(windows)]
415435
#[inline]
416-
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, dst: Q) -> io::Result<()> {
417-
symlink_file(src.as_ref(), &self.std_file, dst.as_ref())
436+
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(
437+
&self,
438+
original: P,
439+
link: Q,
440+
) -> io::Result<()> {
441+
symlink_file(original.as_ref(), &self.std_file, link.as_ref())
418442
}
419443

420444
/// Creates a new directory symlink on a filesystem.
421445
///
446+
/// The `original` argument provides the target of the symlink. The `link`
447+
/// argument provides the name of the created symlink.
448+
///
449+
/// Despite the argument ordering, `original` is not resolved relative to
450+
/// `self` here. `link` is resolved relative to `self`, and `original` is
451+
/// not resolved within this function.
452+
///
453+
/// The `link` path is resolved when the symlink is dereferenced, relative
454+
/// to the directory that contains it.
455+
///
422456
/// This corresponds to [`std::os::windows::fs::symlink_dir`], but only
423457
/// accesses paths relative to `self`.
424458
///
425459
/// [`std::os::windows::fs::symlink_dir`]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html
426460
#[cfg(windows)]
427461
#[inline]
428-
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, dst: Q) -> io::Result<()> {
429-
symlink_dir(src.as_ref(), &self.std_file, dst.as_ref())
462+
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(
463+
&self,
464+
original: P,
465+
link: Q,
466+
) -> io::Result<()> {
467+
symlink_dir(original.as_ref(), &self.std_file, link.as_ref())
430468
}
431469

432470
/// Creates a new `UnixListener` bound to the specified socket.

0 commit comments

Comments
 (0)