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

Implement Borrow and BorrowMut for Matrix #14

Closed
AtheMathmo opened this issue Jul 30, 2016 · 5 comments
Closed

Implement Borrow and BorrowMut for Matrix #14

AtheMathmo opened this issue Jul 30, 2016 · 5 comments

Comments

@AtheMathmo
Copy link
Owner

This is a mirror to rusty-machine/109.

@AtheMathmo
Copy link
Owner Author

Some issues with implementing AsRef/Borrow:

impl<'a, T> AsRef<MatrixSlice<'a, T>> for Matrix<T> {
    fn as_ref(&self) -> &MatrixSlice<'a, T> {
        &self.as_slice()
    }
}

impl<'a, T> AsMut<MatrixSliceMut<'a, T>> for Matrix<T> {
    fn as_mut(&mut self) -> &mut MatrixSliceMut<'a, T> {
        &self.as_mut_slice()
    }
}

This code fails to compile with the following errors:

src/convert.rs:33:15: 33:23 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]
src/convert.rs:33         &self.as_slice()
                                ^~~~~~~~
src/convert.rs:32:5: 34:6 help: consider using an explicit lifetime parameter as shown: fn as_ref(&'a self) -> &MatrixSlice<'a, T>
src/convert.rs:32     fn as_ref(&self) -> &MatrixSlice<'a, T> {
                      ^
src/convert.rs:39:19: 39:31 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]
src/convert.rs:39         &mut self.as_mut_slice()
                                    ^~~~~~~~~~~~
src/convert.rs:38:5: 40:6 help: consider using an explicit lifetime parameter as shown: fn as_mut(&'a mut self) -> &mut MatrixSliceMut<'a, T>
src/convert.rs:38     fn as_mut(&mut self) -> &mut MatrixSliceMut<'a, T> {
                      ^
error: aborting due to 2 previous errors
error: Could not compile `rulinalg`.

But we cannot use an explicit lifetime as it goes against the trait:

error: method `as_ref` has an incompatible type for trait:
 expected bound lifetime parameter ,
    found concrete lifetime [E0053]

Do you see anything obviously wrong with this approach @tafia ? If not I'd propose we make our own internal traits: AsSliceand AsMutSlice. We can implement these for Matrix, MatrixSlice and MatrixSliceMut as appropriate - and use these over our generic functions. It should look something like this:

pub trait AsSlice<T> where T: ?Sized {
    fn as_slice(&self) -> T;
}

(I haven't tested this yet).

@tafia
Copy link
Contributor

tafia commented Aug 1, 2016

Sorry for the late answer. I think a more aggressive change of MatrixSlice might work.
I'll try and see if I get something:

  • have it use a [T] instead of a *const T (it becomes !Size)
  • remove the PhantomData
  • make sure all impl work on &MatrixSlice (Sized)
  • implement AsRef and perhaps Deref as well ( not sure)

@AtheMathmo
Copy link
Owner Author

I'm not sure that I entirely follow how this will solve the problem - but I look forward to seeing if you can pull it off!

Let me know if I can (try to) help at all.

@tafia
Copy link
Contributor

tafia commented Aug 2, 2016

It doesn't worked as well as I was expecting.
I'm trying another approach (#18) to clean up a bit and hopefully it'll be simpler then.

@AtheMathmo
Copy link
Owner Author

I'm closing this issue. We wont be able to implement this in the way we want until Custom DSTs land. In which case we will not be implementing Borrow so much as Deref.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants