Skip to content

Commit b16f73f

Browse files
committed
Document explicit exceptions to str/String's UTF-8 requirements.
1 parent 4e317ea commit b16f73f

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

library/alloc/src/string.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,38 @@ use crate::vec::{self, Vec};
358358
/// called on a `String` may assume that it is valid UTF-8, which means that a non-UTF-8 `String`
359359
/// can lead to undefined behavior down the road.
360360
///
361+
/// As an exception, some functions explicitly allow invalid UTF-8, and will not immediately cause undefined
362+
/// behavior if called on a `String` containing invalid UTF-8. In general, all of `String`'s associated
363+
/// functions other than those listed here should be assumed to require their input be valid UTF-8.
364+
/// Note that calling one of these functions on a `String` containing invalid UTF-8, may result in the return value
365+
/// also containing invalid UTF-8, if relevant.
366+
///
367+
/// * `String::as_bytes`
368+
/// * `String::as_bytes_mut`
369+
/// * `String::as_str`
370+
/// * `String::as_mut_str`
371+
/// * `String::as_ptr`
372+
/// * `String::as_mut_ptr`
373+
/// * `String::as_mut_vec`
374+
/// * `String::capacity`
375+
/// * `String::len`
376+
/// * `String::clear`
377+
/// * `<String as Drop>::drop` (i.e. dropping a `String` that contains invalid UTF-8 does not alone cause UB)
378+
/// * `String::leak`
379+
/// * `String::into_boxed_str`
380+
/// * `String::reserve`
381+
/// * `String::reserve_exact`
382+
/// * `String::try_reserve`
383+
/// * `String::try_reserve_exact`
384+
/// * `<String as Deref>::deref`
385+
/// * `<String as DerefMut>::deref_mut`
386+
/// * `<String as AsRef<str>>::as_ref`
387+
/// * `<String as AsMut<str>>::as_mut`
388+
/// * `<String as Borrow<str>>::borrow`
389+
/// * `<String as BorrowMut<str>>::borrow_mut`
390+
/// * `<String as Clone>::clone`
391+
/// * `<String as Clone>::clone_from`
392+
///
361393
/// [str]: prim@str "str"
362394
/// [`str`]: prim@str "str"
363395
/// [`&str`]: prim@str "&str"

library/core/src/primitive_docs.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,29 @@ mod prim_slice {}
10151015
/// Constructing a non-UTF-8 string slice is not immediate undefined behavior, but any function
10161016
/// called on a string slice may assume that it is valid UTF-8, which means that a non-UTF-8 string
10171017
/// slice can lead to undefined behavior down the road.
1018+
///
1019+
/// As an exception, some functions explicitly allow invalid UTF-8, and will not immediately cause undefined
1020+
/// behavior if called on a `str` containing invalid UTF-8. In general, all of `str`'s associated
1021+
/// functions other than those listed here should be assumed to require their input be valid UTF-8.
1022+
/// Note that calling one of these functions on a `str` containing invalid UTF-8, may result in the return value
1023+
/// also containing invalid UTF-8, if relevant.
1024+
///
1025+
/// * `str::as_bytes`
1026+
/// * `str::as_bytes_mut`
1027+
/// * `str::as_str`
1028+
/// * `str::as_ptr`
1029+
/// * `str::as_mut_ptr`
1030+
/// * `str::bytes`
1031+
/// * `str::into_string`
1032+
/// * `str::into_boxed_bytes`
1033+
/// * `str::len`
1034+
/// * `<str as AsRef<[u8]>>::as_ref`
1035+
/// * `<str as AsRef<str>>::as_ref`
1036+
/// * `<str as AsMut<str>>::as_mut`
1037+
/// * `<str as Borrow<str>>::borrow`
1038+
/// * `<str as BorrowMut<str>>::borrow_mut`
1039+
/// * `<Box<str> as Clone>::clone`
1040+
/// * `<Box<str> as Clone>::clone_from`
10181041
#[stable(feature = "rust1", since = "1.0.0")]
10191042
mod prim_str {}
10201043

0 commit comments

Comments
 (0)