I understand as per a previous discussion that the owned box ~[T] doesn't
quite have the semantics of a unique *pointer*. Below include my successes
in some borrowing scenarios and analogous failed attempts at borrowing a
reference to a unique pointer to an array within a FooVec struct.  How do I
do this?  Is there a borrow() type function/method provided for owned boxes
somewhere? (I see some borrow() stuff within the libstd source, but doesn't
seem to be relevant.)

--------------------------------------------------
fn main() {
    let a : ~[int] = ~[1,2,3];

    // WORKS (borrow1 style below)
    let b : &[int] = a;
}


fn do_borrow<'a, T>(t : &'a T) -> &'a T {
    t
}


struct Foo(~int);

impl Foo {
    fn borrow1<'a>(&'a self) -> &'a int {
        match (self) {
            // error: mismatched types: expected `&'a int` but found `~int`
            // (expected &-ptr but found ~-ptr)
            &Foo(ref v) => *v
        }
    }

    fn borrow2<'a>(&'a self) -> &'a int {
        match (self) {
            // WORKS
            &Foo(ref v) => &**v
        }
    }

    fn borrow3<'a>(&'a self) -> &'a int {
        match (self) {
            // WORKS
            &Foo(ref v) => do_borrow(*v)
        }
    }
}



struct FooVec(~[int]);

impl FooVec {
    fn borrow1<'a>(&'a self) -> &'a [int] {
        match (self) {
            // error: mismatched types: expected `&'a [int]` but found
            // `~[int]` ([] storage differs: expected &'a  but found ~)
            &FooVec(ref v) => *v
        }
    }

    fn borrow2<'a>(&'a self) -> &'a [int] {
        match (self) {
            // error: type ~[int] cannot be dereferenced
            &FooVec(ref v) => &**v
        }
    }

    fn borrow3<'a>(&'a self) -> &'a [int] {
        match (self) {
            // error: mismatched types: expected `&'a [int]` but found
            // `&<V2>` (expected vector but found &-ptr)
            &FooVec(ref v) => do_borrow(*v)
        }
    }
}
--------------------------------------------------
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to