Skip to content

Commit c1e2ea1

Browse files
Rollup merge of rust-lang#52340 - cypher:document-from-trait-in-ffi, r=steveklabnik
Document From trait implementations for OsStr, OsString, CString, and CStr As part of issue rust-lang#51430 (cc @skade). The allocation and copy claims should be double-checked. r? @steveklabnik
2 parents 88fae6a + ed5edcb commit c1e2ea1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/libstd/ffi/c_str.rs

+26
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,12 @@ impl fmt::Debug for CString {
642642

643643
#[stable(feature = "cstring_into", since = "1.7.0")]
644644
impl From<CString> for Vec<u8> {
645+
/// Converts a [`CString`] into a [`Vec`]`<u8>`.
646+
///
647+
/// The conversion consumes the [`CString`], and removes the terminating NUL byte.
648+
///
649+
/// [`Vec`]: ../vec/struct.Vec.html
650+
/// [`CString`]: ../ffi/struct.CString.html
645651
#[inline]
646652
fn from(s: CString) -> Vec<u8> {
647653
s.into_bytes()
@@ -700,6 +706,10 @@ impl<'a> From<&'a CStr> for Box<CStr> {
700706

701707
#[stable(feature = "c_string_from_box", since = "1.18.0")]
702708
impl From<Box<CStr>> for CString {
709+
/// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
710+
///
711+
/// [`Box`]: ../boxed/struct.Box.html
712+
/// [`CString`]: ../ffi/struct.CString.html
703713
#[inline]
704714
fn from(s: Box<CStr>) -> CString {
705715
s.into_c_string()
@@ -716,6 +726,10 @@ impl Clone for Box<CStr> {
716726

717727
#[stable(feature = "box_from_c_string", since = "1.20.0")]
718728
impl From<CString> for Box<CStr> {
729+
/// Converts a [`CString`] into a [`Box`]`<CStr>` without copying or allocating.
730+
///
731+
/// [`CString`]: ../ffi/struct.CString.html
732+
/// [`Box`]: ../boxed/struct.Box.html
719733
#[inline]
720734
fn from(s: CString) -> Box<CStr> {
721735
s.into_boxed_c_str()
@@ -748,6 +762,10 @@ impl<'a> From<&'a CString> for Cow<'a, CStr> {
748762

749763
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
750764
impl From<CString> for Arc<CStr> {
765+
/// Converts a [`CString`] into a [`Arc`]`<CStr>` without copying or allocating.
766+
///
767+
/// [`CString`]: ../ffi/struct.CString.html
768+
/// [`Arc`]: ../sync/struct.Arc.html
751769
#[inline]
752770
fn from(s: CString) -> Arc<CStr> {
753771
let arc: Arc<[u8]> = Arc::from(s.into_inner());
@@ -766,6 +784,10 @@ impl<'a> From<&'a CStr> for Arc<CStr> {
766784

767785
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
768786
impl From<CString> for Rc<CStr> {
787+
/// Converts a [`CString`] into a [`Rc`]`<CStr>` without copying or allocating.
788+
///
789+
/// [`CString`]: ../ffi/struct.CString.html
790+
/// [`Rc`]: ../rc/struct.Rc.html
769791
#[inline]
770792
fn from(s: CString) -> Rc<CStr> {
771793
let rc: Rc<[u8]> = Rc::from(s.into_inner());
@@ -839,6 +861,10 @@ impl fmt::Display for NulError {
839861

840862
#[stable(feature = "rust1", since = "1.0.0")]
841863
impl From<NulError> for io::Error {
864+
/// Converts a [`NulError`] into a [`io::Error`].
865+
///
866+
/// [`NulError`]: ../ffi/struct.NulError.html
867+
/// [`io::Error`]: ../io/struct.Error.html
842868
fn from(_: NulError) -> io::Error {
843869
io::Error::new(io::ErrorKind::InvalidInput,
844870
"data provided contains a nul byte")

src/libstd/ffi/os_str.rs

+22
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ impl OsString {
348348

349349
#[stable(feature = "rust1", since = "1.0.0")]
350350
impl From<String> for OsString {
351+
/// Converts a [`String`] into a [`OsString`].
352+
///
353+
/// The conversion copies the data, and includes an allocation on the heap.
354+
///
355+
/// [`String`]: ../string/struct.String.html
356+
/// [`OsString`]: struct.OsString.html
351357
fn from(s: String) -> OsString {
352358
OsString { inner: Buf::from_string(s) }
353359
}
@@ -630,13 +636,21 @@ impl<'a> From<&'a OsStr> for Box<OsStr> {
630636

631637
#[stable(feature = "os_string_from_box", since = "1.18.0")]
632638
impl From<Box<OsStr>> for OsString {
639+
/// Converts a `Box<OsStr>` into a `OsString` without copying or allocating.
640+
///
641+
/// [`Box`]: ../boxed/struct.Box.html
642+
/// [`OsString`]: ../ffi/struct.OsString.html
633643
fn from(boxed: Box<OsStr>) -> OsString {
634644
boxed.into_os_string()
635645
}
636646
}
637647

638648
#[stable(feature = "box_from_os_string", since = "1.20.0")]
639649
impl From<OsString> for Box<OsStr> {
650+
/// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
651+
///
652+
/// [`Box`]: ../boxed/struct.Box.html
653+
/// [`OsString`]: ../ffi/struct.OsString.html
640654
fn from(s: OsString) -> Box<OsStr> {
641655
s.into_boxed_os_str()
642656
}
@@ -652,6 +666,10 @@ impl Clone for Box<OsStr> {
652666

653667
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
654668
impl From<OsString> for Arc<OsStr> {
669+
/// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
670+
///
671+
/// [`Arc`]: ../sync/struct.Arc.html
672+
/// [`OsString`]: ../ffi/struct.OsString.html
655673
#[inline]
656674
fn from(s: OsString) -> Arc<OsStr> {
657675
let arc = s.inner.into_arc();
@@ -670,6 +688,10 @@ impl<'a> From<&'a OsStr> for Arc<OsStr> {
670688

671689
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
672690
impl From<OsString> for Rc<OsStr> {
691+
/// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
692+
///
693+
/// [`Rc`]: ../rc/struct.Rc.html
694+
/// [`OsString`]: ../ffi/struct.OsString.html
673695
#[inline]
674696
fn from(s: OsString) -> Rc<OsStr> {
675697
let rc = s.inner.into_rc();

0 commit comments

Comments
 (0)