Commit graph

379 commits

Author SHA1 Message Date
Dylan MacKenzie
827251e92b Shorten ownership safety discussion in read_volatile
Non-`Copy` types should not be in volatile memory.
2018-05-09 15:52:16 -07:00
Dylan MacKenzie
e350ba48ed Use the "Safety" heading instead of "Undefined Behavior" 2018-05-09 14:14:43 -07:00
Dylan MacKenzie
d7209d5bab Fix various nits from PR review
- Remove redundant "unsafe" from module description.
- Add a missing `Safety` heading to `read_unaligned`.
- Remove weasel words in `Undefined Behavior` description for
  `write{,_unaligned,_bytes}`.
2018-04-09 14:23:08 -07:00
Dylan MacKenzie
d7ce9a213c Fix broken relative links 2018-04-07 21:33:18 -07:00
Dylan MacKenzie
422b6164e5 Fix broken link in write_unaligned docs 2018-04-07 17:28:10 -07:00
Dylan MacKenzie
6eceb94d09 Don't link "Undefined Behavior" heading
The rendered version does not make clear that this is a link to another
page, and it breaks the anchor link.
2018-04-07 17:22:27 -07:00
Dylan MacKenzie
b564c4a0ee Fix example for ptr::replace 2018-04-07 17:20:20 -07:00
Dylan MacKenzie
8b8091d370 Rewrite docs for std::ptr
- Add links to the GNU libc docs for `memmove`, `memcpy`, and
  `memset`, as well as internally linking to other functions in `std::ptr`
- List sources of UB for all functions.
- Add example to `ptr::drop_in_place` and compares it to `ptr::read`.
- Add examples which more closely mirror real world uses for the
  functions in `std::ptr`. Also, move the reimplementation of `mem::swap`
  to the examples of `ptr::read` and use a more interesting example for
  `copy_nonoverlapping`.
- Change module level description
2018-04-07 15:11:41 -07:00
Josh Triplett
39fe29bf0c src/libcore/ptr.rs: Fix documentation for size of Option<NonNull<T>>
Seems more useful to say that it has the same size as `*mut T`.
2018-03-29 09:46:16 +02:00
bors
39ee3aaa13 Auto merge of #49297 - scottmcm:offset-from, r=dtolnay
Introduce unsafe offset_from on pointers

Adds intrinsics::exact_div to take advantage of the unsafe, which reduces the implementation from
```asm
    sub rcx, rdx
    mov rax, rcx
    sar rax, 63
    shr rax, 62
    lea rax, [rax + rcx]
    sar rax, 2
    ret
```
down to
```asm
    sub rcx, rdx
    sar rcx, 2
    mov rax, rcx
    ret
```
(for `*const i32`)

See discussion on the `offset_to` tracking issue https://github.com/rust-lang/rust/issues/41079

Some open questions
- Would you rather I split the intrinsic PR from the library PR?
- Do we even want the safe version of the API?  https://github.com/rust-lang/rust/issues/41079#issuecomment-374426786  I've added some text to its documentation that even if it's not UB, it's useless to use it between pointers into different objects.

and todos
- [x] ~~I need to make a codegen test~~ Done
- [x] ~~Can the subtraction use nsw/nuw?~~ No, it can't https://github.com/rust-lang/rust/pull/49297#discussion_r176697574
- [x] ~~Should there be `usize` variants of this, like there are now `add` and `sub` that you almost always want over `offset`?  For example, I imagine `sub_ptr` that returns `usize` and where it's UB if the distance is negative.~~ Can wait for later; C gives a signed result https://github.com/rust-lang/rust/issues/41079#issuecomment-375842235, so we might as well, and this existing to go with `offset` makes sense.
2018-03-26 00:15:34 +00:00
Scott McMurray
62649524b9 Fix doctest mutability copy-pasta 2018-03-24 20:41:20 -07:00
Scott McMurray
4a097ea532 Documentation and naming improvements 2018-03-24 20:37:31 -07:00
Scott McMurray
68e0ea9d47 Introduce unsafe offset_from on pointers
Adds intrinsics::exact_div to take advantage of the unsafe, which reduces the implementation from
```asm
    sub rcx, rdx
    mov rax, rcx
    sar rax, 63
    shr rax, 62
    lea rax, [rax + rcx]
    sar rax, 2
    ret
```
down to
```asm
    sub rcx, rdx
    sar rcx, 2
    mov rax, rcx
    ret
```
(for `*const i32`)
2018-03-23 01:30:23 -07:00
Simon Sapin
73c053786d Remove deprecated unstable ptr::Shared type alias.
It has been deprecated for about one release cycle.
2018-03-17 23:59:35 +01:00
Simon Sapin
6d682c9adc Stop using deprecated NonZero APIs
These will eventually be removed
(though the NonZero<T> lang item will likely stay).
2018-03-17 23:07:40 +01:00
Simon Sapin
22f7a02958 Deprecate core::nonzero in favor of ptr::NonNull and num::NonZero* 2018-03-17 23:07:40 +01:00
tinaun
1011b8a3f1 Stabilize Unsafe Pointer Methods
also minor doc fixes.

Closes #43941
2018-03-02 19:20:36 -05:00
Oliver Middleton
45d5a420ad Correct a few stability attributes 2018-02-10 21:20:42 +00:00
Simon Sapin
b8ffc8a3d8 Add an unstable cast<U>() -> NonNull<U> method to NonNull<T>.
This is less verbose than going through raw pointers to cast with `as`.
2018-01-22 09:22:21 +01:00
Simon Sapin
6461c9bdd3 Implement Eq, PartialEq, Ord, PartialOrd, and Hash for NonNull<_> 2018-01-21 10:30:24 +01:00
Simon Sapin
ad37e3fc01 Move Debug for NonNull impl closer to other trait impls 2018-01-21 09:48:58 +01:00
Simon Sapin
3f557947ab NonNull ended up landing in 1.25 2018-01-21 09:48:23 +01:00
Simon Sapin
76b686f78d Rename NonNull::empty to dangling. 2018-01-20 11:09:23 +01:00
Simon Sapin
a1db237cd4 Preserve formatting options in Debug for NonNull/Unique 2018-01-20 11:09:23 +01:00
Simon Sapin
943a9e707c Fix some doc-comment examples for earlier API refactor
https://github.com/rust-lang/rust/pull/41064
2018-01-20 11:09:23 +01:00
Simon Sapin
55c50cd8ac Stabilize std::ptr::NonNull 2018-01-20 11:09:23 +01:00
Simon Sapin
2d51e74580 Remove a deprecated (renamed) and unstable method of NonNull 2018-01-20 11:09:23 +01:00
Simon Sapin
c97c1f7dc3 Mark Unique as perma-unstable, with the feature renamed to ptr_internals. 2018-01-20 11:09:23 +01:00
Simon Sapin
fb03a49c25 Replace Unique<T> with NonZero<T> in Alloc trait 2018-01-20 10:55:16 +01:00
Simon Sapin
f19baf0977 Rename std::ptr::Shared to NonNull
`Shared` is now a deprecated `type` alias.

CC https://github.com/rust-lang/rust/issues/27730#issuecomment-352800629
2018-01-20 10:55:16 +01:00
Corey Farwell
ba5d7a66e8 Implement Debug for ptr::Shared and ptr::Unique.
Fixes https://github.com/rust-lang/rust/issues/46755.
2018-01-20 10:55:16 +01:00
bors
a6fc84440f Auto merge of #46914 - mikeyhew:raw_pointer_self, r=arielb1
Convert warning about `*const _` to a future-compat lint

#46664 was merged before I could convert the soft warning about method lookup on `*const _` into a future-compatibility lint. This PR makes that change.

fixes #46837
tracking issue for the future-compatibility lint: #46906

r? @arielb1
2017-12-25 04:55:57 +00:00
Michael Hewson
60e6629045 fix doctests in libcore 2017-12-23 12:36:04 -05:00
Trevor Spiteri
9d6bd0536a docs: do not call integer overflows as underflows 2017-12-21 02:39:01 +01:00
Simon Sapin
60dc10492c Move PhantomData<T> from Shared<T> to users of both Shared and #[may_dangle]
After discussing [1] today with @pnkfelix and @Gankro,
we concluded that it’s ok for drop checking not to be much smarter
than the current `#[may_dangle]` design which requires an explicit
unsafe opt-in.

[1] https://github.com/rust-lang/rust/issues/27730#issuecomment-316432083
2017-12-16 06:58:16 +01:00
Corey Farwell
f3662275e4 Document behavior of ptr::swap with overlapping regions of memory.
Fixes https://github.com/rust-lang/rust/issues/44479.
2017-12-04 22:49:48 -05:00
kennytm
0e78c29bea Rollup merge of #46287 - SimonSapin:stable-constness, r=aturon
Stabilize const-calling existing const-fns in std

Fixes #46038
2017-11-29 18:37:47 +08:00
Simon Sapin
6c5f53e65e Stabilize const-calling existing const-fns in std
Fixes #46038
2017-11-26 23:43:44 +01:00
Josh Stone
e0f58c6a11
Remove T: Sized on ptr::is_null()
This reverts commit 604f049cd5.

This is purely a revert of cuviper's revert "Restore `T: Sized` on
`ptr::is_null`". So double revert means this is code written by cuviper!
2017-11-19 00:33:56 -08:00
bors
ee2286149a Auto merge of #44932 - cuviper:unsized-ptr-is_null, r=alexcrichton
Remove `T: Sized` on pointer `as_ref()` and `as_mut()`

`NonZero::is_zero()` was already casting all pointers to thin `*mut u8` to check for null.  The same test on unsized fat pointers can also be used with `as_ref()` and `as_mut()` to get fat references.

(This PR formerly changed `is_null()` too, but checking just the data pointer is not obviously correct for trait objects, especially if `*const self` sorts of methods are ever allowed.)
2017-11-07 20:55:01 +00:00
Florian Hartwig
9e966ad3dc Fix references to zero_memory and copy_memory in ptr docs 2017-10-29 15:40:09 +01:00
Alex Crichton
ca18537197 Bump to 1.23 and update bootstrap
This commit updates the bootstrap compiler, bumps the version to 1.23, updates
Cargo, updates books, and updates crates.io dependencies
2017-10-26 13:59:18 -07:00
steveklabnik
f8f9005e57 Fix most rendering warnings from switching to CommonMark 2017-10-20 15:29:35 -04:00
Josh Stone
604f049cd5 Restore T: Sized on ptr::is_null
The exact semantics of `is_null` on unsized pointers are still debatable,
especially for trait objects.  It may be legal to call `*mut self`
methods on a trait object someday, as with Go interfaces, so `is_null`
might need to validate the vtable pointer too.

For `as_ref` and `as_mut`, we're assuming that you cannot have a non-null
data pointer with a null vtable, so casting the unsized check is fine.
2017-10-10 11:35:41 -07:00
bors
417c73891f Auto merge of #44943 - nivkner:fixme_fixup, r=dtolnay
address some FIXME whose associated issues were marked as closed

part of #44366
2017-10-05 19:52:00 +00:00
Niv Kaminer
ff99111f48 address some FIXMEs whose associated issues were marked as closed
remove FIXME(#13101) since `assert_receiver_is_total_eq` stays.
remove FIXME(#19649) now that stability markers render.
remove FIXME(#13642) now the benchmarks were moved.
remove FIXME(#6220) now that floating points can be formatted.
remove FIXME(#18248) and write tests for `Rc<str>` and `Rc<[u8]>`
remove reference to irelevent issues in FIXME(#1697, #2178...)
update FIXME(#5516) to point to getopts issue 7
update FIXME(#7771) to point to RFC 628
update FIXME(#19839) to point to issue 26925
2017-09-30 11:33:47 +03:00
Josh Stone
5c6118339a Document that there are many possible null pointers 2017-09-29 13:59:53 -07:00
Josh Stone
3580c4c589 Remove T: Sized on ptr::is_null(), as_ref(), as_mut()
`NonZero::is_zero()` was already casting all pointers to thin `*mut u8`
to check for null.  It seems reasonable to apply that for `is_null()` in
general, and then unsized fat pointers can also be used with `as_ref()`
and `as_mut()` to get fat references.
2017-09-29 12:36:32 -07:00
Havvy
20fc215323 Normalize spaces in lang attributes. 2017-09-28 01:30:25 -07:00
Oliver Schneider
2787a285bd
Add <*const T>::align_offset and use it in memchr 2017-09-17 21:30:58 +02:00