Skip to content

Commit 7c16f4e

Browse files
committed
refactor(path): delegate implementations where possible
1 parent 5245b81 commit 7c16f4e

File tree

1 file changed

+32
-87
lines changed

1 file changed

+32
-87
lines changed

src/path/arg.rs

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,15 @@ impl Arg for &OsStr {
243243

244244
#[inline]
245245
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
246-
Ok(Cow::Owned(
247-
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
248-
))
246+
self.into_c_str()
249247
}
250248

251249
#[inline]
252250
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
253251
where
254252
Self: 'b,
255253
{
256-
Ok(Cow::Owned(
257-
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
258-
))
254+
return self.as_bytes().into_c_str();
259255
}
260256

261257
#[inline]
@@ -264,7 +260,7 @@ impl Arg for &OsStr {
264260
Self: Sized,
265261
F: FnOnce(&CStr) -> io::Result<T>,
266262
{
267-
with_c_str(self.as_bytes(), f)
263+
return self.as_bytes().into_with_c_str(f);
268264
}
269265
}
270266

@@ -283,10 +279,7 @@ impl Arg for &OsString {
283279

284280
#[inline]
285281
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
286-
Ok(Cow::Owned(
287-
CString::new(OsString::as_os_str(self).as_bytes())
288-
.map_err(|_cstr_err| io::Errno::INVAL)?,
289-
))
282+
self.as_os_str().into_c_str()
290283
}
291284

292285
#[inline]
@@ -303,12 +296,11 @@ impl Arg for &OsString {
303296
Self: Sized,
304297
F: FnOnce(&CStr) -> io::Result<T>,
305298
{
306-
with_c_str(self.as_bytes(), f)
299+
self.as_os_str().into_with_c_str(f)
307300
}
308301
}
309302

310303
#[cfg(feature = "std")]
311-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
312304
impl Arg for OsString {
313305
#[inline]
314306
fn as_str(&self) -> io::Result<&str> {
@@ -322,19 +314,15 @@ impl Arg for OsString {
322314

323315
#[inline]
324316
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
325-
Ok(Cow::Owned(
326-
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
327-
))
317+
self.as_os_str().into_c_str()
328318
}
329319

330320
#[inline]
331321
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
332322
where
333323
Self: 'b,
334324
{
335-
Ok(Cow::Owned(
336-
CString::new(self.into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?,
337-
))
325+
self.into_vec().into_c_str()
338326
}
339327

340328
#[inline]
@@ -343,12 +331,11 @@ impl Arg for OsString {
343331
Self: Sized,
344332
F: FnOnce(&CStr) -> io::Result<T>,
345333
{
346-
f(&CString::new(self.into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?)
334+
f(&self.into_c_str()?)
347335
}
348336
}
349337

350338
#[cfg(feature = "std")]
351-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
352339
impl Arg for &Path {
353340
#[inline]
354341
fn as_str(&self) -> io::Result<&str> {
@@ -362,19 +349,15 @@ impl Arg for &Path {
362349

363350
#[inline]
364351
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
365-
Ok(Cow::Owned(
366-
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
367-
))
352+
self.as_os_str().into_c_str()
368353
}
369354

370355
#[inline]
371356
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
372357
where
373358
Self: 'b,
374359
{
375-
Ok(Cow::Owned(
376-
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
377-
))
360+
self.as_os_str().into_c_str()
378361
}
379362

380363
#[inline]
@@ -383,19 +366,15 @@ impl Arg for &Path {
383366
Self: Sized,
384367
F: FnOnce(&CStr) -> io::Result<T>,
385368
{
386-
with_c_str(self.as_os_str().as_bytes(), f)
369+
self.as_os_str().into_with_c_str(f)
387370
}
388371
}
389372

390373
#[cfg(feature = "std")]
391-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
392374
impl Arg for &PathBuf {
393375
#[inline]
394376
fn as_str(&self) -> io::Result<&str> {
395-
PathBuf::as_path(self)
396-
.as_os_str()
397-
.to_str()
398-
.ok_or(io::Errno::INVAL)
377+
self.as_os_str().to_str().ok_or(io::Errno::INVAL)
399378
}
400379

401380
#[inline]
@@ -405,10 +384,7 @@ impl Arg for &PathBuf {
405384

406385
#[inline]
407386
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
408-
Ok(Cow::Owned(
409-
CString::new(PathBuf::as_path(self).as_os_str().as_bytes())
410-
.map_err(|_cstr_err| io::Errno::INVAL)?,
411-
))
387+
self.as_os_str().into_c_str()
412388
}
413389

414390
#[inline]
@@ -425,12 +401,11 @@ impl Arg for &PathBuf {
425401
Self: Sized,
426402
F: FnOnce(&CStr) -> io::Result<T>,
427403
{
428-
with_c_str(self.as_os_str().as_bytes(), f)
404+
self.as_os_str().into_with_c_str(f)
429405
}
430406
}
431407

432408
#[cfg(feature = "std")]
433-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
434409
impl Arg for PathBuf {
435410
#[inline]
436411
fn as_str(&self) -> io::Result<&str> {
@@ -444,19 +419,15 @@ impl Arg for PathBuf {
444419

445420
#[inline]
446421
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
447-
Ok(Cow::Owned(
448-
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
449-
))
422+
self.as_os_str().into_c_str()
450423
}
451424

452425
#[inline]
453426
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
454427
where
455428
Self: 'b,
456429
{
457-
Ok(Cow::Owned(
458-
CString::new(self.into_os_string().into_vec()).map_err(|_cstr_err| io::Errno::INVAL)?,
459-
))
430+
self.into_os_string().into_c_str()
460431
}
461432

462433
#[inline]
@@ -465,10 +436,7 @@ impl Arg for PathBuf {
465436
Self: Sized,
466437
F: FnOnce(&CStr) -> io::Result<T>,
467438
{
468-
f(
469-
&CString::new(self.into_os_string().into_vec())
470-
.map_err(|_cstr_err| io::Errno::INVAL)?,
471-
)
439+
self.into_os_string().into_with_c_str(f)
472440
}
473441
}
474442

@@ -637,23 +605,18 @@ impl<'a> Arg for Cow<'a, OsStr> {
637605

638606
#[inline]
639607
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
640-
Ok(Cow::Owned(
641-
CString::new(self.as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
642-
))
608+
(&**self).into_c_str()
643609
}
644610

645611
#[inline]
646612
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
647613
where
648614
Self: 'b,
649615
{
650-
Ok(Cow::Owned(
651-
match self {
652-
Cow::Owned(os) => CString::new(os.into_vec()),
653-
Cow::Borrowed(os) => CString::new(os.as_bytes()),
654-
}
655-
.map_err(|_cstr_err| io::Errno::INVAL)?,
656-
))
616+
match self {
617+
Cow::Owned(os) => os.into_c_str(),
618+
Cow::Borrowed(os) => os.into_c_str(),
619+
}
657620
}
658621

659622
#[inline]
@@ -662,7 +625,7 @@ impl<'a> Arg for Cow<'a, OsStr> {
662625
Self: Sized,
663626
F: FnOnce(&CStr) -> io::Result<T>,
664627
{
665-
with_c_str(self.as_bytes(), f)
628+
(&*self).into_with_c_str(f)
666629
}
667630
}
668631

@@ -717,19 +680,15 @@ impl<'a> Arg for Component<'a> {
717680

718681
#[inline]
719682
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
720-
Ok(Cow::Owned(
721-
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
722-
))
683+
self.as_os_str().into_c_str()
723684
}
724685

725686
#[inline]
726687
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
727688
where
728689
Self: 'b,
729690
{
730-
Ok(Cow::Owned(
731-
CString::new(self.as_os_str().as_bytes()).map_err(|_cstr_err| io::Errno::INVAL)?,
732-
))
691+
self.as_os_str().into_c_str()
733692
}
734693

735694
#[inline]
@@ -738,12 +697,11 @@ impl<'a> Arg for Component<'a> {
738697
Self: Sized,
739698
F: FnOnce(&CStr) -> io::Result<T>,
740699
{
741-
with_c_str(self.as_os_str().as_bytes(), f)
700+
self.as_os_str().into_with_c_str(f)
742701
}
743702
}
744703

745704
#[cfg(feature = "std")]
746-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
747705
impl<'a> Arg for Components<'a> {
748706
#[inline]
749707
fn as_str(&self) -> io::Result<&str> {
@@ -757,21 +715,15 @@ impl<'a> Arg for Components<'a> {
757715

758716
#[inline]
759717
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
760-
Ok(Cow::Owned(
761-
CString::new(self.as_path().as_os_str().as_bytes())
762-
.map_err(|_cstr_err| io::Errno::INVAL)?,
763-
))
718+
self.as_path().into_c_str()
764719
}
765720

766721
#[inline]
767722
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
768723
where
769724
Self: 'b,
770725
{
771-
Ok(Cow::Owned(
772-
CString::new(self.as_path().as_os_str().as_bytes())
773-
.map_err(|_cstr_err| io::Errno::INVAL)?,
774-
))
726+
self.as_path().into_c_str()
775727
}
776728

777729
#[inline]
@@ -780,12 +732,11 @@ impl<'a> Arg for Components<'a> {
780732
Self: Sized,
781733
F: FnOnce(&CStr) -> io::Result<T>,
782734
{
783-
with_c_str(self.as_path().as_os_str().as_bytes(), f)
735+
self.as_path().into_with_c_str(f)
784736
}
785737
}
786738

787739
#[cfg(feature = "std")]
788-
#[cfg(any(not(target_os = "wasi"), not(target_env = "p2"), wasip2))]
789740
impl<'a> Arg for Iter<'a> {
790741
#[inline]
791742
fn as_str(&self) -> io::Result<&str> {
@@ -799,21 +750,15 @@ impl<'a> Arg for Iter<'a> {
799750

800751
#[inline]
801752
fn as_cow_c_str(&self) -> io::Result<Cow<'_, CStr>> {
802-
Ok(Cow::Owned(
803-
CString::new(self.as_path().as_os_str().as_bytes())
804-
.map_err(|_cstr_err| io::Errno::INVAL)?,
805-
))
753+
self.as_path().into_c_str()
806754
}
807755

808756
#[inline]
809757
fn into_c_str<'b>(self) -> io::Result<Cow<'b, CStr>>
810758
where
811759
Self: 'b,
812760
{
813-
Ok(Cow::Owned(
814-
CString::new(self.as_path().as_os_str().as_bytes())
815-
.map_err(|_cstr_err| io::Errno::INVAL)?,
816-
))
761+
self.as_path().into_c_str()
817762
}
818763

819764
#[inline]
@@ -822,7 +767,7 @@ impl<'a> Arg for Iter<'a> {
822767
Self: Sized,
823768
F: FnOnce(&CStr) -> io::Result<T>,
824769
{
825-
with_c_str(self.as_path().as_os_str().as_bytes(), f)
770+
self.as_path().into_with_c_str(f)
826771
}
827772
}
828773

0 commit comments

Comments
 (0)