Skip to content

Vec, String: add some missing const-hack comments #137328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ impl String {
pub const fn as_str(&self) -> &str {
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
// at construction.
// FIXME(const-hack): just deref `self` instead
unsafe { str::from_utf8_unchecked(self.vec.as_slice()) }
}

Expand All @@ -1104,6 +1105,7 @@ impl String {
pub const fn as_mut_str(&mut self) -> &mut str {
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
// at construction.
// FIXME(const-hack): just deref `self` instead
unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }
}

Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,7 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct `&mut` references to `self.buf` through `&mut self` methods; borrow-
// check ensures that it is not possible to mutably alias `self.buf` within the
// returned lifetime.
// FIXME(const-hack): just deref `self` instead
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
}

Expand Down Expand Up @@ -1604,6 +1605,7 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct references to `self.buf` through `&self` and `&mut self` methods;
// borrow-check ensures that it is not possible to construct a reference to `self.buf`
// within the returned lifetime.
// FIXME(const-hack): just deref `self` instead
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
}

Expand Down
Loading