Skip to content

Tracking Issue for const conversion methods that don't require const traits #144288

@clarfonthey

Description

@clarfonthey

Feature gate: #![feature(const_convert_methods)]

This is a tracking issue for various methods used in the constification of the Deref and DerefMut traits which could be stabilised before const traits are stabilised. These methods themselves do not depend on any traits in their APIs and can be implemented in a way that does not use unstable const traits.

Public API

Constness is added to the following methods:

  • Box::into_boxed_slice (Box<T> -> Box<[T]>)
  • Box::into_pin (Box<T> -> Pin<Box<T>>)
  • CStr::as_bytes
  • CStr::as_bytes_with_nul
  • CString::as_c_str
  • CString::into_bytes
  • CString::into_bytes_with_nul
  • CString::into_string
  • IntoStringError::into_cstring
  • IntoStringError::utf8_error
  • <Box<OsStr>>::into_os_string
  • OsString::as_os_str
  • <Box<Path>>::into_path_buf
  • PathBuf::as_path
  • path::{Component, PrefixComponent}::as_os_str
  • <Box<str>>::into_boxed_bytes
  • <Box<str>>::into_string
  • String::from_utf8
  • String::from_utf8_unchecked
  • FromUtf8Error::as_bytes
  • FromUtf8Error::into_bytes
  • FromUtf8Error::utf8_error
  • Box<[T]>::into_vec

rust-lang/libs-team#624 also proposes adding the following methods which do not exist. These are currently private, but could be made public:

  • OsString::as_mut_os_str
  • PathBuf::as_mut_path

Additionally, that ACP also proposes adding CString::as_mut_c_str, which is not implemented at all.

Explicitly Excluded APIs

While conversions from boxes to other collections are supported, because the reverse direction involves removing excess capacity, those conversions are omitted.

Although CString currently uses Box<[u8]> internally, this is an implementation detail that isn't relied upon. However, it also does prevent conversions from Vec for similar reasons.

Explicitly because the nul byte on CStrings can be popped off without deallocating, CString::into_bytes is made const.

Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Unresolved Questions

  • Should CString be given a DerefMut impl?
  • Should PrefixComponent be given AsRef<OsStr> and AsRef<Path> impls? (these are indirectly used for Component's implementation, but it doesn't have them itself)

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions