Skip to main content

MessageBuffers

Struct MessageBuffers 

Source
pub struct MessageBuffers { /* private fields */ }
Expand description

Reusable storage for crate::MessageParser::parse_with.

A parsed message keeps its parts, headers and values in a handful of vectors. Parsing many messages one after the other (an mbox or Maildir import, classifier training, a batch of messages from a queue) can reuse those vectors instead of allocating new ones for every message: pass the buffers to parse_with, and when done with the message, get them back with crate::Message::into_buffers. Once the vectors have grown to fit the messages, parsing allocates nothing.

There is no hidden state: dropping a message instead of calling into_buffers only means the next parse allocates, and dropping the buffers frees the memory they hold.

The vectors never shrink on their own: they keep the capacity of the largest message parsed with them, so one very large message in a batch holds its memory for the rest of the loop. MessageBuffers::shrink_to bounds that memory between two parses.

use mail_parser::{MessageBuffers, MessageParser, mailbox::mbox::MessageIterator};

let mbox = b"From a@example.com Sat Jan  3 01:05:34 1996\n\
Subject: first\n\nbody\n\
From b@example.com Sat Jan  3 01:05:35 1996\n\
Subject: second\n\nbody\n";

let parser = MessageParser::new();
let mut buffers = MessageBuffers::new();
let mut subjects = Vec::new();
for entry in MessageIterator::new(&mbox[..]) {
    let entry = entry.expect("readable mbox");
    if let Some(message) = parser.parse_with(entry.contents(), &mut buffers) {
        subjects.push(message.subject().unwrap_or_default().to_string());
        buffers = message.into_buffers();
    }
}
assert_eq!(subjects, ["first", "second"]);

Implementations§

Source§

impl MessageBuffers

Source

pub fn new() -> Self

Empty buffers; they grow to fit the messages parsed with them.

Source

pub fn shrink_to(&mut self, max_bytes: usize)

Frees the memory held by the buffers when it exceeds max_bytes, so that they hold at most max_bytes afterwards; the next parse then allocates again. Call it between two parses, for example after crate::Message::into_buffers in a batch loop, to keep one large message from pinning its memory. It costs nothing when the buffers are within the bound.

use mail_parser::{MessageBuffers, MessageParser};

let parser = MessageParser::new();
let mut buffers = MessageBuffers::new();
for raw in [&b"Subject: small\n\nbody\n"[..], b"Subject: next\n\nbody\n"] {
    if let Some(message) = parser.parse_with(raw, &mut buffers) {
        assert!(message.subject().is_some());
        buffers = message.into_buffers();
        buffers.shrink_to(1 << 20);
    }
}

Trait Implementations§

Source§

impl Clone for MessageBuffers

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MessageBuffers

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MessageBuffers

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.