Skip to content

Commit 60bc76f

Browse files
jeremyletangalexcrichton
authored andcommitted
remove duplicate function from std::ptr (is_null, is_not_null, offset, mut_offset)
1 parent 1c5295c commit 60bc76f

File tree

14 files changed

+70
-97
lines changed

14 files changed

+70
-97
lines changed

src/doc/guide-ffi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<T: Send> Unique<T> {
195195
pub fn new(value: T) -> Unique<T> {
196196
unsafe {
197197
let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;
198-
assert!(!ptr::is_null(ptr));
198+
assert!(!ptr.is_null());
199199
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
200200
// move_val_init moves a value into this memory without
201201
// attempting to drop the original value.

src/libarena/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::cast;
3434
use std::cell::{Cell, RefCell};
3535
use std::mem;
3636
use std::num;
37-
use std::ptr;
3837
use std::kinds::marker;
3938
use std::rc::Rc;
4039
use std::rt::global_heap;
@@ -144,7 +143,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
144143
let fill = chunk.fill.get();
145144

146145
while idx < fill {
147-
let tydesc_data: *uint = transmute(ptr::offset(buf, idx as int));
146+
let tydesc_data: *uint = transmute(buf.offset(idx as int));
148147
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
149148
let (size, align) = ((*tydesc).size, (*tydesc).align);
150149

@@ -155,7 +154,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
155154
//debug!("freeing object: idx = {}, size = {}, align = {}, done = {}",
156155
// start, size, align, is_done);
157156
if is_done {
158-
((*tydesc).drop_glue)(ptr::offset(buf, start as int) as *i8);
157+
((*tydesc).drop_glue)(buf.offset(start as int) as *i8);
159158
}
160159

161160
// Find where the next tydesc lives
@@ -261,7 +260,7 @@ impl Arena {
261260
// start, n_bytes, align, head.fill);
262261

263262
let buf = self.head.as_ptr();
264-
return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int));
263+
return (buf.offset(tydesc_start as int), buf.offset(start as int));
265264
}
266265
}
267266

src/libextra/c_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl <T> CVec<T> {
119119
pub fn get<'a>(&'a self, ofs: uint) -> &'a T {
120120
assert!(ofs < self.len);
121121
unsafe {
122-
&*ptr::mut_offset(self.base, ofs as int)
122+
&*self.base.offset(ofs as int)
123123
}
124124
}
125125

@@ -131,7 +131,7 @@ impl <T> CVec<T> {
131131
pub fn get_mut<'a>(&'a mut self, ofs: uint) -> &'a mut T {
132132
assert!(ofs < self.len);
133133
unsafe {
134-
&mut *ptr::mut_offset(self.base, ofs as int)
134+
&mut *self.base.offset(ofs as int)
135135
}
136136
}
137137

src/librustc/metadata/loader.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use std::io;
3030
use std::num;
3131
use std::option;
3232
use std::os::consts::{macos, freebsd, linux, android, win32};
33-
use std::ptr;
3433
use std::str;
3534
use std::vec;
3635
use flate;
@@ -340,7 +339,7 @@ fn get_metadata_section_imp(os: Os, filename: &Path) -> Option<MetadataBlob> {
340339
});
341340
if !version_ok { return None; }
342341

343-
let cvbuf1 = ptr::offset(cvbuf, vlen as int);
342+
let cvbuf1 = cvbuf.offset(vlen as int);
344343
debug!("inflating {} bytes of compressed metadata",
345344
csz - vlen);
346345
vec::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {

src/librustc/middle/trans/builder.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::cast;
2121
use std::hashmap::HashMap;
2222
use std::libc::{c_uint, c_ulonglong, c_char};
2323
use syntax::codemap::Span;
24-
use std::ptr::is_not_null;
2524

2625
pub struct Builder<'a> {
2726
llbuilder: BuilderRef,
@@ -492,7 +491,7 @@ impl<'a> Builder<'a> {
492491
debug!("Store {} -> {}",
493492
self.ccx.tn.val_to_str(val),
494493
self.ccx.tn.val_to_str(ptr));
495-
assert!(is_not_null(self.llbuilder));
494+
assert!(self.llbuilder.is_not_null());
496495
self.count_insn("store");
497496
unsafe {
498497
llvm::LLVMBuildStore(self.llbuilder, val, ptr);
@@ -503,7 +502,7 @@ impl<'a> Builder<'a> {
503502
debug!("Store {} -> {}",
504503
self.ccx.tn.val_to_str(val),
505504
self.ccx.tn.val_to_str(ptr));
506-
assert!(is_not_null(self.llbuilder));
505+
assert!(self.llbuilder.is_not_null());
507506
self.count_insn("store.volatile");
508507
unsafe {
509508
let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);

src/librustuv/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ mod test {
426426
unsafe {
427427
let base = transmute::<*u8, *mut u8>(buf.base);
428428
(*base) = 1;
429-
(*ptr::mut_offset(base, 1)) = 2;
429+
(*base.offset(1)) = 2;
430430
}
431431

432432
assert!(slice[0] == 1);

src/libserialize/ebml.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ pub mod reader {
131131
}
132132

133133
pub fn vuint_at(data: &[u8], start: uint) -> Res {
134-
use std::ptr::offset;
135134
use std::mem::from_be32;
136135

137136
if data.len() - start < 4 {
@@ -163,7 +162,7 @@ pub mod reader {
163162

164163
unsafe {
165164
let (ptr, _): (*u8, uint) = transmute(data);
166-
let ptr = offset(ptr, start as int);
165+
let ptr = ptr.offset(start as int);
167166
let ptr: *i32 = transmute(ptr);
168167
let val = from_be32(*ptr) as u32;
169168

src/libstd/c_str.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'a> ToCStr for &'a [u8] {
310310
let buf = malloc_raw(self_len + 1);
311311

312312
ptr::copy_memory(buf, self.as_ptr(), self_len);
313-
*ptr::mut_offset(buf, self_len as int) = 0;
313+
*buf.offset(self_len as int) = 0;
314314

315315
CString::new(buf as *libc::c_char, true)
316316
}
@@ -368,7 +368,7 @@ impl<'a> Iterator<libc::c_char> for CChars<'a> {
368368
if ch == 0 {
369369
None
370370
} else {
371-
self.ptr = unsafe { ptr::offset(self.ptr, 1) };
371+
self.ptr = unsafe { self.ptr.offset(1) };
372372
Some(ch)
373373
}
374374
}
@@ -429,18 +429,18 @@ mod tests {
429429
fn test_str_to_c_str() {
430430
"".to_c_str().with_ref(|buf| {
431431
unsafe {
432-
assert_eq!(*ptr::offset(buf, 0), 0);
432+
assert_eq!(*buf.offset(0), 0);
433433
}
434434
});
435435

436436
"hello".to_c_str().with_ref(|buf| {
437437
unsafe {
438-
assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
439-
assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
440-
assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
441-
assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
442-
assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
443-
assert_eq!(*ptr::offset(buf, 5), 0);
438+
assert_eq!(*buf.offset(0), 'h' as libc::c_char);
439+
assert_eq!(*buf.offset(1), 'e' as libc::c_char);
440+
assert_eq!(*buf.offset(2), 'l' as libc::c_char);
441+
assert_eq!(*buf.offset(3), 'l' as libc::c_char);
442+
assert_eq!(*buf.offset(4), 'o' as libc::c_char);
443+
assert_eq!(*buf.offset(5), 0);
444444
}
445445
})
446446
}
@@ -450,28 +450,28 @@ mod tests {
450450
let b: &[u8] = [];
451451
b.to_c_str().with_ref(|buf| {
452452
unsafe {
453-
assert_eq!(*ptr::offset(buf, 0), 0);
453+
assert_eq!(*buf.offset(0), 0);
454454
}
455455
});
456456

457457
let _ = bytes!("hello").to_c_str().with_ref(|buf| {
458458
unsafe {
459-
assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
460-
assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
461-
assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
462-
assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
463-
assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
464-
assert_eq!(*ptr::offset(buf, 5), 0);
459+
assert_eq!(*buf.offset(0), 'h' as libc::c_char);
460+
assert_eq!(*buf.offset(1), 'e' as libc::c_char);
461+
assert_eq!(*buf.offset(2), 'l' as libc::c_char);
462+
assert_eq!(*buf.offset(3), 'l' as libc::c_char);
463+
assert_eq!(*buf.offset(4), 'o' as libc::c_char);
464+
assert_eq!(*buf.offset(5), 0);
465465
}
466466
});
467467

468468
let _ = bytes!("foo", 0xff).to_c_str().with_ref(|buf| {
469469
unsafe {
470-
assert_eq!(*ptr::offset(buf, 0), 'f' as libc::c_char);
471-
assert_eq!(*ptr::offset(buf, 1), 'o' as libc::c_char);
472-
assert_eq!(*ptr::offset(buf, 2), 'o' as libc::c_char);
473-
assert_eq!(*ptr::offset(buf, 3), 0xff as i8);
474-
assert_eq!(*ptr::offset(buf, 4), 0);
470+
assert_eq!(*buf.offset(0), 'f' as libc::c_char);
471+
assert_eq!(*buf.offset(1), 'o' as libc::c_char);
472+
assert_eq!(*buf.offset(2), 'o' as libc::c_char);
473+
assert_eq!(*buf.offset(3), 0xff as i8);
474+
assert_eq!(*buf.offset(4), 0);
475475
}
476476
});
477477
}
@@ -634,16 +634,15 @@ mod bench {
634634
use extra::test::BenchHarness;
635635
use libc;
636636
use prelude::*;
637-
use ptr;
638637

639638
#[inline]
640639
fn check(s: &str, c_str: *libc::c_char) {
641640
let s_buf = s.as_ptr();
642641
for i in range(0, s.len()) {
643642
unsafe {
644643
assert_eq!(
645-
*ptr::offset(s_buf, i as int) as libc::c_char,
646-
*ptr::offset(c_str, i as int));
644+
*s_buf.offset(i as int) as libc::c_char,
645+
*c_str.offset(i as int));
647646
}
648647
}
649648
}

src/libstd/io/extensions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use iter::Iterator;
1818
use option::Option;
1919
use io::Reader;
2020
use vec::{OwnedVector, ImmutableVector};
21+
use ptr::RawPtr;
2122

2223
/// An iterator that reads a single byte on each iteration,
2324
/// until `.read_byte()` returns `None`.
@@ -104,7 +105,7 @@ pub fn u64_from_be_bytes(data: &[u8],
104105
start: uint,
105106
size: uint)
106107
-> u64 {
107-
use ptr::{copy_nonoverlapping_memory, offset, mut_offset};
108+
use ptr::{copy_nonoverlapping_memory};
108109
use mem::from_be64;
109110
use vec::MutableVector;
110111

@@ -116,9 +117,9 @@ pub fn u64_from_be_bytes(data: &[u8],
116117

117118
let mut buf = [0u8, ..8];
118119
unsafe {
119-
let ptr = offset(data.as_ptr(), start as int);
120+
let ptr = data.as_ptr().offset(start as int);
120121
let out = buf.as_mut_ptr();
121-
copy_nonoverlapping_memory(mut_offset(out, (8 - size) as int), ptr, size);
122+
copy_nonoverlapping_memory(out.offset((8 - size) as int), ptr, size);
122123
from_be64(*(out as *i64)) as u64
123124
}
124125
}

src/libstd/ptr.rs

+9-34
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,6 @@ use unstable::intrinsics;
2121

2222
#[cfg(not(test))] use cmp::{Eq, Ord};
2323

24-
/// Calculate the offset from a pointer.
25-
/// The `count` argument is in units of T; e.g. a `count` of 3
26-
/// represents a pointer offset of `3 * sizeof::<T>()` bytes.
27-
#[inline]
28-
pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
29-
intrinsics::offset(ptr, count)
30-
}
31-
32-
/// Calculate the offset from a mut pointer. The count *must* be in bounds or
33-
/// otherwise the loads of this address are undefined.
34-
/// The `count` argument is in units of T; e.g. a `count` of 3
35-
/// represents a pointer offset of `3 * sizeof::<T>()` bytes.
36-
#[inline]
37-
pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
38-
intrinsics::offset(ptr as *T, count) as *mut T
39-
}
40-
4124
/// Return the offset of the first null pointer in `buf`.
4225
#[inline]
4326
pub unsafe fn buf_len<T>(buf: **T) -> uint {
@@ -63,7 +46,7 @@ impl<T> Clone for *mut T {
6346
pub unsafe fn position<T>(buf: *T, f: |&T| -> bool) -> uint {
6447
let mut i = 0;
6548
loop {
66-
if f(&(*offset(buf, i as int))) { return i; }
49+
if f(&(*buf.offset(i as int))) { return i; }
6750
else { i += 1; }
6851
}
6952
}
@@ -76,14 +59,6 @@ pub fn null<T>() -> *T { 0 as *T }
7659
#[inline]
7760
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
7861

79-
/// Returns true if the pointer is equal to the null pointer.
80-
#[inline]
81-
pub fn is_null<T,P:RawPtr<T>>(ptr: P) -> bool { ptr.is_null() }
82-
83-
/// Returns true if the pointer is not equal to the null pointer.
84-
#[inline]
85-
pub fn is_not_null<T,P:RawPtr<T>>(ptr: P) -> bool { ptr.is_not_null() }
86-
8762
/**
8863
* Copies data from one location to another.
8964
*
@@ -206,7 +181,7 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
206181
}
207182
//let start_ptr = *arr;
208183
for e in range(0, len) {
209-
let n = offset(arr, e as int);
184+
let n = arr.offset(e as int);
210185
cb(*n);
211186
}
212187
debug!("array_each_with_len: after iterate");
@@ -278,7 +253,7 @@ impl<T> RawPtr<T> for *T {
278253
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
279254
/// the object, or one-byte-past-the-end.
280255
#[inline]
281-
unsafe fn offset(self, count: int) -> *T { offset(self, count) }
256+
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) }
282257
}
283258

284259
/// Extension methods for mutable pointers
@@ -323,7 +298,7 @@ impl<T> RawPtr<T> for *mut T {
323298
/// This method should be preferred over `offset` when the guarantee can be
324299
/// satisfied, to enable better optimization.
325300
#[inline]
326-
unsafe fn offset(self, count: int) -> *mut T { mut_offset(self, count) }
301+
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T }
327302
}
328303

329304
// Equality for pointers
@@ -478,14 +453,14 @@ pub mod ptr_tests {
478453
let v0 = ~[32000u16, 32001u16, 32002u16];
479454
let mut v1 = ~[0u16, 0u16, 0u16];
480455

481-
copy_memory(mut_offset(v1.as_mut_ptr(), 1),
482-
offset(v0.as_ptr(), 1), 1);
456+
copy_memory(v1.as_mut_ptr().offset(1),
457+
v0.as_ptr().offset(1), 1);
483458
assert!((v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16));
484459
copy_memory(v1.as_mut_ptr(),
485-
offset(v0.as_ptr(), 2), 1);
460+
v0.as_ptr().offset(2), 1);
486461
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
487462
v1[2] == 0u16));
488-
copy_memory(mut_offset(v1.as_mut_ptr(), 2),
463+
copy_memory(v1.as_mut_ptr().offset(2),
489464
v0.as_ptr(), 1u);
490465
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
491466
v1[2] == 32000u16));
@@ -525,7 +500,7 @@ pub mod ptr_tests {
525500
assert!(p.is_null());
526501
assert!(!p.is_not_null());
527502

528-
let q = unsafe { offset(p, 1) };
503+
let q = unsafe { p.offset(1) };
529504
assert!(!q.is_null());
530505
assert!(q.is_not_null());
531506

src/libstd/repr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use io;
2323
use iter::Iterator;
2424
use option::{Some, None, Option};
2525
use ptr;
26+
use ptr::RawPtr;
2627
use reflect;
2728
use reflect::{MovePtr, align};
2829
use result::{Ok, Err};
@@ -221,7 +222,7 @@ impl<'a> ReprVisitor<'a> {
221222
if_ok!(self, self.writer.write(", ".as_bytes()));
222223
}
223224
self.visit_ptr_inner(p as *u8, inner);
224-
p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8;
225+
p = align(unsafe { p.offset(sz as int) as uint }, al) as *u8;
225226
left -= dec;
226227
}
227228
if_ok!(self, self.writer.write([']' as u8]));

0 commit comments

Comments
 (0)