bincode-org / bincode

A binary encoder / decoder implementation in Rust.
MIT License
2.67k stars 265 forks source link

BorrowDecode not implemented for slices of borrowed elements #718

Closed kirawi closed 1 month ago

kirawi commented 4 months ago

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]
stale[bot] commented 2 months ago

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.

VictorKoenders commented 2 months ago

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.