Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. pub struct Image {
  2. pub name: String,
  3. bytes: Vec<u8>,
  4. }
  5.  
  6. impl Image {
  7. pub fn new(name: String, bytes: &[u8]) -> Image {
  8. Image {
  9. name,
  10. bytes: bytes.to_vec(),
  11. }
  12. }
  13. pub fn bytes(&self) -> impl Iterator<Item = u8> {
  14. // If i return the self.bytes.iter() I get an iterator over &u8's (because we're operating on a borrow of Image)
  15. // It seems to me that it would be better to return an iterator of u8's instead
  16. // (and the most efficient way to do that is probably to clone the vector before constructing the iterator?)
  17.  
  18. // However i want to know why THIS doesnt work:
  19. // This should construct an iterator with an internal borrow of self, which derefrences each value as it's returned by the iterator
  20. // Does this just not work because i cannot communicate to the caller that the returned value has a borrow on self?
  21. // If this was a type that let me specify a lifetime parameter would it work if i specified that both `self` and the return value has the lifetime 'a?
  22. self.bytes.iter().map(|byte| *byte)
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement