Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw pointer coercion #361

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions active/0000-raw-pointer-coercion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
- Start Date: (fill me in with today's date, YYYY-MM-DD)
- RFC PR #: (leave this empty)
- Rust Issue #: (leave this empty)

# Summary

`*mut T` and `&mut T` should coerce to `*const T`.

# Motivation

Precedence:

- `&mut T` coerces to `&T`
- `&T` coerces to `*const T`
- `&mut T` coerces to `*mut T`

Converting from `*mut` to `*const` is a safe operation which happens regularly in FFI code:

```c
T *new(void);
void non_mutating_operation(T const *);
void mutating_operation(T *);
```

```rust
struct TWrapper {
raw: *mut T,
}

impl TWrapper {
fn non_mutating_operation(&self) {
unsafe { non_mutating_operation(self.raw as *const T); }
}

fn mutating_operation(&mut self) {
unsafe { mutating_operation(self.raw); }
}
}
```

# Drawbacks

None right now.

# Detailed design

Add these rules:

- `*mut T` coerces to `*const T`
- `&mut T` coerces to `*const T`

So that the following code compiles:

```rust
let a = &mut 1u;
let b: *mut uint = a;

let x: *const uint = a;
let y: *const uint = b;
```

# Alternatives

Leave it the way it is.

# Unresolved questions

None right now.