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

Object safety for traits #163

Closed
tailhook opened this issue Nov 10, 2017 · 3 comments
Closed

Object safety for traits #163

tailhook opened this issue Nov 10, 2017 · 3 comments
Milestone

Comments

@tailhook
Copy link

Currently, rust complains:

error[E0038]: the trait `bytes::Buf` cannot be made into an object
  --> src/proto.rs:17:5
   |
17 |     buf: Option<Box<Buf>>,
   |     ^^^^^^^^^^^^^^^^^^^^^ the trait `bytes::Buf` cannot be made into an object
   |
   = note: method `get_u16` has generic type parameters
   = note: method `get_i16` has generic type parameters
   = note: method `get_u32` has generic type parameters
   = note: method `get_i32` has generic type parameters
   = note: method `get_u64` has generic type parameters
   = note: method `get_i64` has generic type parameters
   = note: method `get_uint` has generic type parameters
   = note: method `get_int` has generic type parameters
   = note: method `get_f32` has generic type parameters
   = note: method `get_f64` has generic type parameters

While I'm not sure if boxing buf is good for performance reasons, I'd prefer that boxing be allowed for flexibility.

This can be done in in two ways:

  1. Use different methods get_u16_le and get_u16_be
  2. Making a wrapper type: buf.little_endian_reader().get_u16()
    While slightly less convenient, I still think it's beneficial to make a trait object-safe.

This can only be done as a breaking change in 0.5 though.

@carllerche carllerche added this to the v0.5 milestone Dec 14, 2017
@carllerche
Copy link
Member

One option is to add where clauses to all these fns, but then they would not be available as part of the trait object.

Adding _be and _le suffixes to the trait is the better option here.

@tailhook
Copy link
Author

Is adding where clauses a backward-compatible change? I.e. can we add where clauses deprecate the methods and add new ones with suffixes?

@carllerche
Copy link
Member

I... think? But @alexcrichton would know better.

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