Skip to content

Commit

Permalink
switch to returning mutable refs
Browse files Browse the repository at this point in the history
fixes #2

With rust-lang/rust#27616 fixed, we're able to do this.

Not expected to break any users as &mut is coerced to & without issue.
Tests passed unchanged.

May want to consider adding tests to check for mutability.
  • Loading branch information
codyps committed Feb 11, 2017
1 parent 95bdfef commit c1f89be
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use std::mem;
* in rust's borrow checker causing soundness issues. Details are in the RFC linked above.
*/
pub trait Leak<T : ?Sized> {
fn leak<'a>(self) -> &'a T where T: 'a;
fn leak<'a>(self) -> &'a mut T where T: 'a;
}

impl<T : ?Sized> Leak<T> for Box<T> {
fn leak<'a>(self) -> &'a T where T: 'a {
fn leak<'a>(self) -> &'a mut T where T: 'a {
let r = Self::into_raw(self);
unsafe { &mut *r }
}
Expand All @@ -34,15 +34,15 @@ impl<T : ?Sized> Leak<T> for Box<T> {
*/

impl Leak<str> for String {
fn leak<'a>(mut self) -> &'a str where Self: 'a {
fn leak<'a>(mut self) -> &'a mut str where Self: 'a {
let r: *mut str = &mut self[..];
mem::forget(self);
unsafe { &mut *r }
}
}

impl<T> Leak<[T]> for Vec<T> {
fn leak<'a>(mut self) -> &'a [T] where [T]: 'a {
fn leak<'a>(mut self) -> &'a mut [T] where [T]: 'a {
let r: *mut [T] = &mut self[..];
mem::forget(self);
unsafe { &mut *r }
Expand Down

0 comments on commit c1f89be

Please sign in to comment.