Skip to content

Replace rental with ouroboros #44

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async-native-tls = { version = "0.3.3" }
async-std = { version = "1.8.0", default-features = false, features = ["std"] }
pin-utils = "0.1.0-alpha.4"
futures = "0.3.0"
rental = "0.5.5"
ouroboros = "0.9"
stop-token = "0.2"
byte-pool = "0.2.2"
lazy_static = "1.4.0"
Expand Down
6 changes: 3 additions & 3 deletions src/imap_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<R: Read + Write + Unpin> ImapStream<R> {
let block: Block<'static> = self.buffer.take_block();
// Be aware, now self.buffer is invalid until block is returned or reset!

let res = ResponseData::try_new(block, |buf| {
let res = ResponseData::try_new_or_recover(block, |buf| {
let buf = &buf[..self.buffer.used()];
log::trace!("decode: input: {:?}", std::str::from_utf8(buf));
match imap_proto::parser::parse_response(buf) {
Expand Down Expand Up @@ -139,8 +139,8 @@ impl<R: Read + Write + Unpin> ImapStream<R> {
});
match res {
Ok(response) => Ok(Some(response)),
Err(rental::RentalError(err, block)) => {
self.buffer.return_block(block);
Err((err, heads)) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Voultapher returning the owner seems to be missing from self_cell

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit out of context. I suspect you are looking into using self_cell instead of ouroboros and some feature is missing. Please elaborate.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in this case with self_cell you would just get err back, not block/heads

Looking at the source it seems like self_cell just drops everything inside the cell when an error occurs

self.buffer.return_block(heads.raw);
match err {
Some(err) => Err(err),
None => Ok(None),
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
#[macro_use]
extern crate pin_utils;

#[macro_use]
extern crate rental;

// Reexport imap_proto for easier access.
pub use imap_proto;

Expand Down
26 changes: 10 additions & 16 deletions src/types/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ use imap_proto::{MailboxDatum, Response};

use crate::types::ResponseData;

rental! {
pub mod rents {
use super::*;

/// A name that matches a `LIST` or `LSUB` command.
#[rental(debug, covariant)]
pub struct Name {
response: Box<ResponseData>,
inner: InnerName<'response>,
}
}
/// A name that matches a `LIST` or `LSUB` command.
#[ouroboros::self_referencing(pub_extras)]
pub struct Name {
response: Box<ResponseData>,
#[borrows(response)]
#[covariant]
inner: InnerName<'this>,
}

#[derive(PartialEq, Eq, Debug)]
Expand All @@ -24,8 +20,6 @@ pub struct InnerName<'a> {
name: &'a str,
}

pub use rents::Name;

/// An attribute set for an IMAP name.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum NameAttribute<'a> {
Expand Down Expand Up @@ -100,22 +94,22 @@ impl Name {

/// Attributes of this name.
pub fn attributes(&self) -> &[NameAttribute<'_>] {
&self.suffix().attributes[..]
&self.borrow_inner().attributes[..]
}

/// The hierarchy delimiter is a character used to delimit levels of hierarchy in a mailbox
/// name. A client can use it to create child mailboxes, and to search higher or lower levels
/// of naming hierarchy. All children of a top-level hierarchy node use the same
/// separator character. `None` means that no hierarchy exists; the name is a "flat" name.
pub fn delimiter(&self) -> Option<&str> {
self.suffix().delimiter
self.borrow_inner().delimiter
}

/// The name represents an unambiguous left-to-right hierarchy, and are valid for use as a
/// reference in `LIST` and `LSUB` commands. Unless [`NameAttribute::NoSelect`] is indicated,
/// the name is also valid as an argument for commands, such as `SELECT`, that accept mailbox
/// names.
pub fn name(&self) -> &str {
self.suffix().name
self.borrow_inner().name
}
}
26 changes: 10 additions & 16 deletions src/types/response_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ use std::fmt;
use byte_pool::Block;
use imap_proto::{RequestId, Response};

rental! {
pub mod rents {
use super::*;

#[rental(covariant)]
pub struct ResponseData {
raw: Block<'static>,
response: Response<'raw>,
}
}
#[ouroboros::self_referencing(pub_extras)]
pub struct ResponseData {
raw: Block<'static>,
#[borrows(raw)]
#[covariant]
response: Response<'this>,
}

pub use rents::ResponseData;

impl std::cmp::PartialEq for ResponseData {
fn eq(&self, other: &Self) -> bool {
self.parsed() == other.parsed()
Expand All @@ -28,21 +22,21 @@ impl std::cmp::Eq for ResponseData {}
impl fmt::Debug for ResponseData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ResponseData")
.field("raw", &self.head().len())
.field("response", self.suffix())
.field("raw", &self.borrow_raw().len())
.field("response", self.borrow_response())
.finish()
}
}

impl ResponseData {
pub fn request_id(&self) -> Option<&RequestId> {
match self.suffix() {
match self.borrow_response() {
Response::Done { ref tag, .. } => Some(tag),
_ => None,
}
}

pub fn parsed(&self) -> &Response<'_> {
self.suffix()
self.borrow_response()
}
}