Skip to content

Commit c28b5fc

Browse files
committed
---
yaml --- r: 152937 b: refs/heads/try2 c: 2c9aada h: refs/heads/master i: 152935: 1daf5f5 v: v3
1 parent e98a42a commit c28b5fc

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 6a3695d54f14491c39da7910082df9ba9c833b37
8+
refs/heads/try2: 2c9aada10cd9146138d76d182ccbd2a7627df204
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustrt/c_str.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,79 @@ impl CString {
133133
c_str.buf
134134
}
135135

136+
/// Return a pointer to the NUL-terminated string data.
137+
///
138+
/// `.as_ptr` returns an internal pointer into the `CString`, and
139+
/// may be invalidated when the `CString` falls out of scope (the
140+
/// destructor will run, freeing the allocation if there is
141+
/// one).
142+
///
143+
/// ```rust
144+
/// let foo = "some string";
145+
///
146+
/// // right
147+
/// let x = foo.to_c_str();
148+
/// let p = x.as_ptr();
149+
///
150+
/// // wrong (the CString will be freed, invalidating `p`)
151+
/// let p = foo.to_c_str().as_ptr();
152+
/// ```
153+
///
154+
/// # Failure
155+
///
156+
/// Fails if the CString is null.
157+
///
158+
/// # Example
159+
///
160+
/// ```rust
161+
/// extern crate libc;
162+
///
163+
/// fn main() {
164+
/// let c_str = "foo bar".to_c_str();
165+
/// unsafe {
166+
/// libc::puts(c_str.as_ptr());
167+
/// }
168+
/// }
169+
/// ```
170+
pub fn as_ptr(&self) -> *const libc::c_char {
171+
if self.buf.is_null() { fail!("CString is null!"); }
172+
173+
self.buf
174+
}
175+
176+
/// Return a mutable pointer to the NUL-terminated string data.
177+
///
178+
/// `.as_mut_ptr` returns an internal pointer into the `CString`, and
179+
/// may be invalidated when the `CString` falls out of scope (the
180+
/// destructor will run, freeing the allocation if there is
181+
/// one).
182+
///
183+
/// ```rust
184+
/// let foo = "some string";
185+
///
186+
/// // right
187+
/// let mut x = foo.to_c_str();
188+
/// let p = x.as_mut_ptr();
189+
///
190+
/// // wrong (the CString will be freed, invalidating `p`)
191+
/// let p = foo.to_c_str().as_mut_ptr();
192+
/// ```
193+
///
194+
/// # Failure
195+
///
196+
/// Fails if the CString is null.
197+
pub fn as_mut_ptr(&mut self) -> *mut libc::c_char {
198+
if self.buf.is_null() { fail!("CString is null!") }
199+
200+
self.buf as *mut _
201+
}
202+
136203
/// Calls a closure with a reference to the underlying `*libc::c_char`.
137204
///
138205
/// # Failure
139206
///
140207
/// Fails if the CString is null.
208+
#[deprecated="use `.as_ptr()`"]
141209
pub fn with_ref<T>(&self, f: |*const libc::c_char| -> T) -> T {
142210
if self.buf.is_null() { fail!("CString is null!"); }
143211
f(self.buf)
@@ -148,6 +216,7 @@ impl CString {
148216
/// # Failure
149217
///
150218
/// Fails if the CString is null.
219+
#[deprecated="use `.as_mut_ptr()`"]
151220
pub fn with_mut_ref<T>(&mut self, f: |*mut libc::c_char| -> T) -> T {
152221
if self.buf.is_null() { fail!("CString is null!"); }
153222
f(self.buf as *mut libc::c_char)

0 commit comments

Comments
 (0)