Skip to content

str::as_str but for CStr, OsStr, Path, and [T] #643

@GrigorenkoPV

Description

@GrigorenkoPV

Proposal

Add CStr::as_c_str, OsStr::as_os_str, Path::as_path, [T]::as_slice, and [T]::as_mut_slice, analogous to str::as_str (rust-lang/rust#130366) to aid dereferencing Box<CStr/OsStr/...> without CString, OsString, ... being the only ones with the convenient .as_thing() method.

Background

str::as_str was proposed by t-libs-api in rust-lang/rust#129550 (comment) as a response to a PR adding Box<str>::as_str.

The idea of analogous additions for other "slicy" types was brought up in rust-lang/rust#130366 (comment)

Solution sketch

rust-lang/rust#145933

Questions

  • Should this reuse the same feature gate as str::as_str?
  • Should the [T]:: methods be moved to a separate feature gate?
  • Bikeshed the feature gate names.
  • Technically slices already have an as_slice method, but with no mut version and it comes from an unstable trait with a closed tracking issue and docs saying that hopefully this trait will be removed in the future: https://doc.rust-lang.org/stable/std/primitive.slice.html#impl-SlicePattern-for-%5BT%5D

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions