bincode-org/bincode

BorrowDecode not implemented for slices of borrowed elements

kirawi opened this issue · 2 comments

I have this code:

use std::time::SystemTime;

use bincode::BorrowDecode;

/// A written Work (novel/comic/etc.). It can contain text and images
// TODO: See if this is even possible w/ borrow checker and sled's db
#[derive(BorrowDecode)]
pub struct LibEntry<'db> {
    pub title: &'db str,
    pub description: &'db str,
    pub chapters: &'db [Chapter<'db>],

    pub authors: &'db [&'db str],

    // Dates
    pub publish: SystemTime,
    pub update: SystemTime,
}

#[derive(BorrowDecode)]
pub struct Chapter<'db> {
    pub title: &'db str,
    pub elements: &'db [Entry<'db>],
}

#[derive(BorrowDecode)]
pub enum Entry<'db> {
    Paragraph(&'db str),
    Image(&'db [u8]),
}

Which results in errors like:

the trait bound `&[Chapter<'_>]: BorrowDecode<'_>` is not satisfied
the following other types implement trait `BorrowDecode<'de>`:
  &'a [u8]
  [T; N]
the trait bound `&[&str]: BorrowDecode<'_>` is not satisfied
the following other types implement trait `BorrowDecode<'de>`:
  &'a [u8]
  [T; N]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This is not possible with borrowed decode. We have no way of knowing during decoding that &[T] is perfectly the same in-memory, except if this is a u8 for which we have an exception. Please use a Vec<&str> instead.