Skip to content

Commit

Permalink
c_str: move .unwrap & document it more clearly.
Browse files Browse the repository at this point in the history
This should be called rarely, but it was placed first in the list of
methods, making it very tempting to call.
  • Loading branch information
huonw committed Jun 29, 2014
1 parent d4d4bc4 commit 569f13a
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/librustrt/c_str.rs
Expand Up @@ -122,17 +122,6 @@ impl CString {
CString { buf: buf, owns_buffer_: owns_buffer }
}

/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
///
/// The original object is destructed after this method is called, and if
/// the underlying pointer was previously allocated, care must be taken to
/// ensure that it is deallocated properly.
pub unsafe fn unwrap(self) -> *const libc::c_char {
let mut c_str = self;
c_str.owns_buffer_ = false;
c_str.buf
}

/// Return a pointer to the NUL-terminated string data.
///
/// `.as_ptr` returns an internal pointer into the `CString`, and
Expand Down Expand Up @@ -289,6 +278,22 @@ impl CString {
marker: marker::ContravariantLifetime,
}
}

/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
///
/// Any ownership of the buffer by the `CString` wrapper is
/// forgotten, meaning that the backing allocation of this
/// `CString` is not automatically freed if it owns the
/// allocation. In this case, a user of `.unwrap()` should ensure
/// the allocation is freed, to avoid leaking memory.
///
/// Prefer `.as_ptr()` when just retrieving a pointer to the
/// string data, as that does not relinquish ownership.
pub unsafe fn unwrap(mut self) -> *const libc::c_char {
self.owns_buffer_ = false;
self.buf
}

}

impl Drop for CString {
Expand Down

5 comments on commit 569f13a

@bors
Copy link
Contributor

@bors bors commented on 569f13a Jun 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@569f13a

@bors
Copy link
Contributor

@bors bors commented on 569f13a Jun 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/cstr-remove-withref = 569f13a into auto

@bors
Copy link
Contributor

@bors bors commented on 569f13a Jun 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/cstr-remove-withref = 569f13a merged ok, testing candidate = b569c77

@bors
Copy link
Contributor

@bors bors commented on 569f13a Jun 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = b569c77

Please sign in to comment.