Browse Source

Add dyn keyword to Trait objects

And fix some unused var warnings as well
tags/pre-alpha-0.3.0
Manos Pitsidianakis 2 years ago
parent
commit
ecb3fd7f3d
Signed by untrusted user: epilys GPG Key ID: 73627C2F690DF710
  1. 27
      melib/src/backends.rs
  2. 4
      melib/src/backends/imap.rs
  3. 7
      melib/src/backends/imap/watch.rs
  4. 4
      melib/src/backends/maildir/backend.rs
  5. 6
      melib/src/backends/mbox.rs
  6. 14
      melib/src/email.rs
  7. 2
      melib/src/email/attachments.rs
  8. 2
      melib/src/email/compose.rs
  9. 2
      melib/src/email/parser.rs
  10. 3
      melib/src/thread.rs
  11. 2
      ui/src/components/contacts/contact_list.rs
  12. 3
      ui/src/components/indexer/index.rs
  13. 6
      ui/src/components/mail/listing/compact.rs
  14. 8
      ui/src/components/mail/listing/thread.rs
  15. 4
      ui/src/components/mail/view.rs
  16. 4
      ui/src/components/mail/view/envelope.rs
  17. 26
      ui/src/components/utilities.rs
  18. 2
      ui/src/components/utilities/widgets.rs
  19. 4
      ui/src/conf/accounts.rs
  20. 2
      ui/src/execute/actions.rs
  21. 6
      ui/src/state.rs

27
melib/src/backends.rs

@ -37,12 +37,13 @@ use std::ops::Deref;
use fnv::FnvHashMap;
use std;
pub type BackendCreator = Box<Fn(&AccountSettings, Box<Fn(&str) -> bool>) -> Box<MailBackend>>;
pub type BackendCreator =
Box<dyn Fn(&AccountSettings, Box<dyn Fn(&str) -> bool>) -> Box<dyn MailBackend>>;
/// A hashmap containing all available mail backends.
/// An abstraction over any available backends.
pub struct Backends {
map: FnvHashMap<std::string::String, Box<Fn() -> BackendCreator>>,
map: FnvHashMap<std::string::String, Box<dyn Fn() -> BackendCreator>>,
}
impl Default for Backends {
@ -78,7 +79,7 @@ impl Backends {
self.map[key]()
}
pub fn register(&mut self, key: String, backend: Box<Fn() -> BackendCreator>) {
pub fn register(&mut self, key: String, backend: Box<dyn Fn() -> BackendCreator>) {
if self.map.contains_key(&key) {
panic!("{} is an already registered backend", key);
}
@ -116,9 +117,9 @@ impl RefreshEvent {
/// A `RefreshEventConsumer` is a boxed closure that must be used to consume a `RefreshEvent` and
/// send it to a UI provided channel. We need this level of abstraction to provide an interface for
/// all users of mailbox refresh events.
pub struct RefreshEventConsumer(Box<Fn(RefreshEvent) -> () + Send + Sync>);
pub struct RefreshEventConsumer(Box<dyn Fn(RefreshEvent) -> () + Send + Sync>);
impl RefreshEventConsumer {
pub fn new(b: Box<Fn(RefreshEvent) -> () + Send + Sync>) -> Self {
pub fn new(b: Box<dyn Fn(RefreshEvent) -> () + Send + Sync>) -> Self {
RefreshEventConsumer(b)
}
pub fn send(&self, r: RefreshEvent) {
@ -126,7 +127,7 @@ impl RefreshEventConsumer {
}
}
pub struct NotifyFn(Box<Fn(FolderHash) -> () + Send + Sync>);
pub struct NotifyFn(Box<dyn Fn(FolderHash) -> () + Send + Sync>);
impl fmt::Debug for NotifyFn {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -134,14 +135,14 @@ impl fmt::Debug for NotifyFn {
}
}
impl From<Box<Fn(FolderHash) -> () + Send + Sync>> for NotifyFn {
fn from(kind: Box<Fn(FolderHash) -> () + Send + Sync>) -> Self {
impl From<Box<dyn Fn(FolderHash) -> () + Send + Sync>> for NotifyFn {
fn from(kind: Box<dyn Fn(FolderHash) -> () + Send + Sync>) -> Self {
NotifyFn(kind)
}
}
impl NotifyFn {
pub fn new(b: Box<Fn(FolderHash) -> () + Send + Sync>) -> Self {
pub fn new(b: Box<dyn Fn(FolderHash) -> () + Send + Sync>) -> Self {
NotifyFn(b)
}
pub fn notify(&self, f: FolderHash) {
@ -164,10 +165,10 @@ pub trait MailBackend: ::std::fmt::Debug {
fn get(&mut self, folder: &Folder) -> Async<Result<Vec<Envelope>>>;
fn watch(&self, sender: RefreshEventConsumer) -> Result<()>;
fn folders(&self) -> FnvHashMap<FolderHash, Folder>;
fn operation(&self, hash: EnvelopeHash, folder_hash: FolderHash) -> Box<BackendOp>;
fn operation(&self, hash: EnvelopeHash, folder_hash: FolderHash) -> Box<dyn BackendOp>;
fn save(&self, bytes: &[u8], folder: &str, flags: Option<Flag>) -> Result<()>;
fn folder_operation(&mut self, path: &str, op: FolderOperation) -> Result<()> {
fn folder_operation(&mut self, _path: &str, _op: FolderOperation) -> Result<()> {
Ok(())
}
}
@ -234,11 +235,11 @@ pub trait BackendOp: ::std::fmt::Debug + ::std::marker::Send {
/// Seen flag when fetching an envelope)
#[derive(Debug)]
pub struct ReadOnlyOp {
op: Box<BackendOp>,
op: Box<dyn BackendOp>,
}
impl ReadOnlyOp {
pub fn new(op: Box<BackendOp>) -> Box<BackendOp> {
pub fn new(op: Box<dyn BackendOp>) -> Box<dyn BackendOp> {
Box::new(ReadOnlyOp { op })
}
}

4
melib/src/backends/imap.rs

@ -245,7 +245,7 @@ impl MailBackend for ImapType {
.collect()
}
fn operation(&self, hash: EnvelopeHash, _folder_hash: FolderHash) -> Box<BackendOp> {
fn operation(&self, hash: EnvelopeHash, _folder_hash: FolderHash) -> Box<dyn BackendOp> {
let (uid, folder_hash) = self.hash_index.lock().unwrap()[&hash];
Box::new(ImapOp::new(
uid,
@ -400,7 +400,7 @@ macro_rules! exit_on_error {
}
}
impl ImapType {
pub fn new(s: &AccountSettings, is_subscribed: Box<Fn(&str) -> bool>) -> Self {
pub fn new(s: &AccountSettings, is_subscribed: Box<dyn Fn(&str) -> bool>) -> Self {
use std::io::prelude::*;
use std::net::TcpStream;
debug!(s);

7
melib/src/backends/imap/watch.rs

@ -57,7 +57,7 @@ pub fn poll_with_examine(kit: ImapWatchKit) {
let mut response = String::with_capacity(8 * 1024);
loop {
std::thread::sleep(std::time::Duration::from_millis(5 * 60 * 1000));
for (hash, folder) in &folders {
for folder in folders.values() {
examine_updates(folder, &sender, &mut conn, &hash_index, &uid_index);
}
let mut main_conn = main_conn.lock().unwrap();
@ -318,11 +318,6 @@ pub fn idle(kit: ImapWatchKit) {
}
}
}
debug!("failure");
sender.send(RefreshEvent {
hash: folder_hash,
kind: RefreshEventKind::Failure(MeliError::new("conn_error")),
});
}
fn examine_updates(

4
melib/src/backends/maildir/backend.rs

@ -460,7 +460,7 @@ impl MailBackend for MaildirType {
Ok(())
}
fn operation(&self, hash: EnvelopeHash, folder_hash: FolderHash) -> Box<BackendOp> {
fn operation(&self, hash: EnvelopeHash, folder_hash: FolderHash) -> Box<dyn BackendOp> {
Box::new(MaildirOp::new(hash, self.hash_indexes.clone(), folder_hash))
}
@ -529,7 +529,7 @@ impl MailBackend for MaildirType {
}
impl MaildirType {
pub fn new(settings: &AccountSettings, is_subscribed: Box<Fn(&str) -> bool>) -> Self {
pub fn new(settings: &AccountSettings, is_subscribed: Box<dyn Fn(&str) -> bool>) -> Self {
let mut folders: FnvHashMap<FolderHash, MaildirFolder> = Default::default();
fn recurse_folders<P: AsRef<Path>>(
folders: &mut FnvHashMap<FolderHash, MaildirFolder>,

6
melib/src/backends/mbox.rs

@ -242,7 +242,7 @@ impl BackendOp for MboxOp {
flags
}
fn set_flag(&mut self, envelope: &mut Envelope, flag: Flag) -> Result<()> {
fn set_flag(&mut self, _envelope: &mut Envelope, _flag: Flag) -> Result<()> {
Ok(())
}
}
@ -526,7 +526,7 @@ impl MailBackend for MboxType {
.map(|(h, f)| (*h, f.clone() as Folder))
.collect()
}
fn operation(&self, hash: EnvelopeHash, _folder_hash: FolderHash) -> Box<BackendOp> {
fn operation(&self, hash: EnvelopeHash, _folder_hash: FolderHash) -> Box<dyn BackendOp> {
let (offset, length) = {
let index = self.index.lock().unwrap();
index[&hash]
@ -540,7 +540,7 @@ impl MailBackend for MboxType {
}
impl MboxType {
pub fn new(s: &AccountSettings, _is_subscribed: Box<Fn(&str) -> bool>) -> Self {
pub fn new(s: &AccountSettings, _is_subscribed: Box<dyn Fn(&str) -> bool>) -> Self {
let path = Path::new(s.root_folder.as_str());
if !path.exists() {
panic!(

14
melib/src/email.rs

@ -387,7 +387,7 @@ impl Envelope {
}
Err(MeliError::new("Couldn't parse mail."))
}
pub fn from_token(mut operation: Box<BackendOp>, hash: EnvelopeHash) -> Option<Envelope> {
pub fn from_token(mut operation: Box<dyn BackendOp>, hash: EnvelopeHash) -> Option<Envelope> {
let mut e = Envelope::new(hash);
e.flags = operation.fetch_flags();
if let Ok(bytes) = operation.as_bytes() {
@ -522,7 +522,7 @@ impl Envelope {
Ok(())
}
pub fn populate_headers_from_token(&mut self, mut operation: Box<BackendOp>) -> Result<()> {
pub fn populate_headers_from_token(&mut self, mut operation: Box<dyn BackendOp>) -> Result<()> {
let headers = operation.fetch_headers()?;
self.populate_headers(headers)
}
@ -563,7 +563,7 @@ impl Envelope {
let _strings: Vec<String> = self.to.iter().map(|a| format!("{}", a)).collect();
_strings.join(", ")
}
pub fn bytes(&self, mut operation: Box<BackendOp>) -> Vec<u8> {
pub fn bytes(&self, mut operation: Box<dyn BackendOp>) -> Vec<u8> {
operation
.as_bytes()
.map(|v| v.into())
@ -608,7 +608,7 @@ impl Envelope {
})
})
}
pub fn body(&self, mut operation: Box<BackendOp>) -> Attachment {
pub fn body(&self, mut operation: Box<dyn BackendOp>) -> Attachment {
debug!("searching body for {:?}", self.message_id_display());
let file = operation.as_bytes();
self.body_bytes(file.unwrap())
@ -781,7 +781,7 @@ impl Envelope {
pub fn set_datetime(&mut self, new_val: chrono::DateTime<chrono::FixedOffset>) {
self.timestamp = new_val.timestamp() as UnixTimestamp;
}
pub fn set_flag(&mut self, f: Flag, mut operation: Box<BackendOp>) -> Result<()> {
pub fn set_flag(&mut self, f: Flag, mut operation: Box<dyn BackendOp>) -> Result<()> {
self.flags.toggle(f);
operation.set_flag(self, f)?;
Ok(())
@ -792,14 +792,14 @@ impl Envelope {
pub fn flags(&self) -> Flag {
self.flags
}
pub fn set_seen(&mut self, operation: Box<BackendOp>) -> Result<()> {
pub fn set_seen(&mut self, operation: Box<dyn BackendOp>) -> Result<()> {
if !self.flags.contains(Flag::SEEN) {
self.set_flag(Flag::SEEN, operation)
} else {
Ok(())
}
}
pub fn set_unseen(&mut self, operation: Box<BackendOp>) -> Result<()> {
pub fn set_unseen(&mut self, operation: Box<dyn BackendOp>) -> Result<()> {
if self.flags.contains(Flag::SEEN) {
self.set_flag(Flag::SEEN, operation)
} else {

2
melib/src/email/attachments.rs

@ -441,7 +441,7 @@ fn decode_rfc822(_raw: &[u8]) -> Attachment {
builder.build()
}
type Filter<'a> = Box<FnMut(&'a Attachment, &mut Vec<u8>) -> () + 'a>;
type Filter<'a> = Box<dyn FnMut(&'a Attachment, &mut Vec<u8>) -> () + 'a>;
fn decode_rec_helper<'a>(a: &'a Attachment, filter: &mut Option<Filter<'a>>) -> Vec<u8> {
match a.content_type {

2
melib/src/email/compose.rs

@ -113,7 +113,7 @@ impl str::FromStr for Draft {
}
impl Draft {
pub fn edit(envelope: &Envelope, mut op: Box<BackendOp>) -> Self {
pub fn edit(envelope: &Envelope, mut op: Box<dyn BackendOp>) -> Self {
let mut ret = Draft::default();
//TODO: Inform user if error
{

2
melib/src/email/parser.rs

@ -247,7 +247,7 @@ pub fn headers_raw(input: &[u8]) -> IResult<&[u8], &[u8]> {
if input.is_empty() {
return IResult::Incomplete(Needed::Unknown);
}
for (i, x) in input.iter().enumerate() {
for i in 0..input.len() {
if input[i..].starts_with(b"\n\n") {
return IResult::Done(&input[(i + 1)..], &input[0..=i]);
} else if input[i..].starts_with(b"\r\n\r\n") {

3
melib/src/thread.rs

@ -1107,6 +1107,7 @@ impl Threads {
}
}
/*
fn link_envelope(&mut self, envelope: &mut Envelope) {
let t_idx: ThreadHash = {
let m_id = envelope.message_id().raw();
@ -1215,7 +1216,7 @@ impl Threads {
}
ref_ptr = parent_id;
}
}
}*/
fn tree_insert_root(&mut self, new_id: ThreadHash, envelopes: &Envelopes) {
debug_assert!(

2
ui/src/components/contacts/contact_list.rs

@ -22,7 +22,7 @@ pub struct ContactList {
mode: ViewMode,
dirty: bool,
view: Option<Box<Component>>,
view: Option<Box<dyn Component>>,
id: ComponentId,
}

3
ui/src/components/indexer/index.rs

@ -33,7 +33,7 @@ pub struct Index {
dirty: bool,
state: IndexState,
content: Box<IndexContent>,
content: Box<dyn IndexContent>,
id: ComponentId,
}
@ -117,7 +117,6 @@ impl Component for Index {
IndexState::Unfocused => {
self.content.draw(grid, area, context);
}
//IndexState::Search => unreachable!(),
}
self.dirty = false;

6
ui/src/components/mail/listing/compact.rs

@ -112,11 +112,6 @@ impl ListingTrait for CompactListing {
return;
}
let i = self.get_envelope_under_cursor(idx, context);
let account = &context.accounts[self.cursor_pos.0];
let is_seen = {
let root_envelope: &Envelope = &account.get_env(&i);
root_envelope.is_seen()
};
let fg_color = self.data_columns.columns[0][(0, idx)].fg();
let bg_color = if self.cursor_pos.2 == idx {
@ -128,7 +123,6 @@ impl ListingTrait for CompactListing {
};
let (upper_left, bottom_right) = area;
let (width, height) = self.data_columns.columns[3].size();
change_colors(grid, area, fg_color, bg_color);
let mut x = get_x(upper_left)
+ self.data_columns.widths[0]

8
ui/src/components/mail/listing/thread.rs

@ -502,12 +502,8 @@ impl Component for ThreadListing {
false
} else {
let account = &mut context.accounts[self.cursor_pos.0];
let (hash, is_seen) = {
let envelope: &Envelope =
&account.get_env(&self.locations[self.cursor_pos.2]);
(envelope.hash(), envelope.is_seen())
};
is_seen
let envelope: &Envelope = &account.get_env(&self.locations[self.cursor_pos.2]);
envelope.is_seen()
}
};

4
ui/src/components/mail/view.rs

@ -68,7 +68,7 @@ impl ViewMode {
pub struct MailView {
coordinates: (usize, usize, EnvelopeHash),
pager: Option<Pager>,
subview: Option<Box<Component>>,
subview: Option<Box<dyn Component>>,
dirty: bool,
mode: ViewMode,
expand_headers: bool,
@ -89,7 +89,7 @@ impl MailView {
pub fn new(
coordinates: (usize, usize, EnvelopeHash),
pager: Option<Pager>,
subview: Option<Box<Component>>,
subview: Option<Box<dyn Component>>,
) -> Self {
MailView {
coordinates,

4
ui/src/components/mail/view/envelope.rs

@ -48,7 +48,7 @@ impl ViewMode {
#[derive(Debug)]
pub struct EnvelopeView {
pager: Option<Pager>,
subview: Option<Box<Component>>,
subview: Option<Box<dyn Component>>,
dirty: bool,
mode: ViewMode,
wrapper: EnvelopeWrapper,
@ -69,7 +69,7 @@ impl EnvelopeView {
pub fn new(
wrapper: EnvelopeWrapper,
pager: Option<Pager>,
subview: Option<Box<Component>>,
subview: Option<Box<dyn Component>>,
account_pos: usize,
) -> Self {
EnvelopeView {

26
ui/src/components/utilities.rs

@ -30,8 +30,8 @@ pub use self::widgets::*;
/// A horizontally split in half container.
#[derive(Debug)]
pub struct HSplit {
top: Box<Component>,
bottom: Box<Component>,
top: Box<dyn Component>,
bottom: Box<dyn Component>,
show_divider: bool,
ratio: usize, // bottom/whole height * 100
id: ComponentId,
@ -46,8 +46,8 @@ impl fmt::Display for HSplit {
impl HSplit {
pub fn new(
top: Box<Component>,
bottom: Box<Component>,
top: Box<dyn Component>,
bottom: Box<dyn Component>,
ratio: usize,
show_divider: bool,
) -> Self {
@ -125,8 +125,8 @@ impl Component for HSplit {
/// A vertically split in half container.
#[derive(Debug)]
pub struct VSplit {
left: Box<Component>,
right: Box<Component>,
left: Box<dyn Component>,
right: Box<dyn Component>,
show_divider: bool,
prev_visibility: (bool, bool),
/// This is the width of the right container to the entire width.
@ -143,8 +143,8 @@ impl fmt::Display for VSplit {
impl VSplit {
pub fn new(
left: Box<Component>,
right: Box<Component>,
left: Box<dyn Component>,
right: Box<dyn Component>,
ratio: usize,
show_divider: bool,
) -> Self {
@ -602,7 +602,7 @@ impl Component for Pager {
/// Status bar.
#[derive(Debug)]
pub struct StatusBar {
container: Box<Component>,
container: Box<dyn Component>,
status: String,
notifications: VecDeque<String>,
ex_buffer: Field,
@ -624,7 +624,7 @@ impl fmt::Display for StatusBar {
}
impl StatusBar {
pub fn new(container: Box<Component>) -> Self {
pub fn new(container: Box<dyn Component>) -> Self {
StatusBar {
container,
status: String::with_capacity(256),
@ -1120,7 +1120,7 @@ impl Component for Progress {
#[derive(Debug)]
pub struct Tabbed {
pinned: usize,
children: Vec<Box<Component>>,
children: Vec<Box<dyn Component>>,
cursor_pos: usize,
show_shortcuts: bool,
@ -1130,7 +1130,7 @@ pub struct Tabbed {
}
impl Tabbed {
pub fn new(children: Vec<Box<Component>>) -> Self {
pub fn new(children: Vec<Box<dyn Component>>) -> Self {
let pinned = children.len();
Tabbed {
pinned,
@ -1194,7 +1194,7 @@ impl Tabbed {
context.dirty_areas.push_back(area);
}
pub fn add_component(&mut self, new: Box<Component>) {
pub fn add_component(&mut self, new: Box<dyn Component>) {
self.children.push(new);
}
}

2
ui/src/components/utilities/widgets.rs

@ -1,7 +1,7 @@
use super::*;
use fnv::FnvHashMap;
type AutoCompleteFn = Box<Fn(&Context, &str) -> Vec<AutoCompleteEntry> + Send>;
type AutoCompleteFn = Box<dyn Fn(&Context, &str) -> Vec<AutoCompleteEntry> + Send>;
#[derive(Debug, PartialEq)]
enum FormFocus {

4
ui/src/conf/accounts.rs

@ -317,7 +317,7 @@ impl Account {
}
fn new_worker(
folder: Folder,
backend: &mut Box<MailBackend>,
backend: &mut Box<dyn MailBackend>,
notify_fn: Arc<NotifyFn>,
) -> Worker {
let mailbox_handle = backend.get(&folder);
@ -617,7 +617,7 @@ impl Account {
pub fn contains_key(&self, h: EnvelopeHash) -> bool {
self.collection.contains_key(&h)
}
pub fn operation(&self, h: EnvelopeHash) -> Box<BackendOp> {
pub fn operation(&self, h: EnvelopeHash) -> Box<dyn BackendOp> {
for mailbox in self.folders.values() {
match mailbox {
MailboxEntry::Available(ref m) | MailboxEntry::Parsing(ref m, _, _) => {

2
ui/src/execute/actions.rs

@ -45,7 +45,7 @@ pub enum ListingAction {
#[derive(Debug)]
pub enum TabAction {
New(Option<Box<Component>>),
New(Option<Box<dyn Component>>),
NewDraft(usize, Option<Draft>),
Reply((usize, usize, usize), ThreadHash), // thread coordinates (account, mailbox, root_set idx) and thread hash
Close,

6
ui/src/state.rs

@ -21,7 +21,7 @@
/*! The application's state.
The UI crate has an Box<Component>-Component-System design. The System part, is also the application's state, so they're both merged in the `State` struct.
The UI crate has an Box<dyn Component>-Component-System design. The System part, is also the application's state, so they're both merged in the `State` struct.
`State` owns all the Components of the UI. In the application's main event loop, input is handed to the state in the form of `UIEvent` objects which traverse the component graph. Components decide to handle each input or not.
@ -128,7 +128,7 @@ pub struct State {
stdout: Option<StateStdout>,
child: Option<ForkType>,
pub mode: UIMode,
components: Vec<Box<Component>>,
components: Vec<Box<dyn Component>>,
pub context: Context,
threads: FnvHashMap<thread::ThreadId, (chan::Sender<bool>, thread::JoinHandle<()>)>,
work_controller: WorkController,
@ -477,7 +477,7 @@ impl State {
);
}
}
pub fn register_component(&mut self, component: Box<Component>) {
pub fn register_component(&mut self, component: Box<dyn Component>) {
self.components.push(component);
}
/// Convert user commands to actions/method calls.

Loading…
Cancel
Save