Skip to content
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

Use dyn for trait objects #58

Merged
merged 1 commit into from
Jun 1, 2019
Merged
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
12 changes: 6 additions & 6 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ impl<Term: Terminal> Interface<Term> {
}

/// Returns a clone of the current completer instance.
pub fn completer(&self) -> Arc<Completer<Term>> {
pub fn completer(&self) -> Arc<dyn Completer<Term>> {
self.lock_reader().completer().clone()
}

/// Replaces the current completer, returning the previous instance.
pub fn set_completer(&self, completer: Arc<Completer<Term>>)
-> Arc<Completer<Term>> {
pub fn set_completer(&self, completer: Arc<dyn Completer<Term>>)
-> Arc<dyn Completer<Term>> {
self.lock_reader().set_completer(completer)
}

Expand Down Expand Up @@ -277,15 +277,15 @@ impl<Term: Terminal> Interface<Term> {
/// this is not a requirement.
///
/// Returns the function previously defined with the same name.
pub fn define_function<T>(&self, name: T, cmd: Arc<Function<Term>>)
-> Option<Arc<Function<Term>>> where T: Into<Cow<'static, str>> {
pub fn define_function<T>(&self, name: T, cmd: Arc<dyn Function<Term>>)
-> Option<Arc<dyn Function<Term>>> where T: Into<Cow<'static, str>> {
self.lock_reader().define_function(name, cmd)
}

/// Removes a function defined with the given name.
///
/// Returns the defined function.
pub fn remove_function(&self, name: &str) -> Option<Arc<Function<Term>>> {
pub fn remove_function(&self, name: &str) -> Option<Arc<dyn Function<Term>>> {
self.lock_reader().remove_function(name)
}

Expand Down
8 changes: 4 additions & 4 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ impl Terminal for MemoryTerminal {

fn name(&self) -> &str { "memory-terminal" }

fn lock_read<'a>(&'a self) -> Box<TerminalReader<Self> + 'a> {
fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a> {
Box::new(MemoryReadGuard(self.lock_reader()))
}

fn lock_write<'a>(&'a self) -> Box<TerminalWriter<Self> + 'a> {
fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a> {
Box::new(MemoryWriteGuard(self.lock_writer()))
}
}
Expand All @@ -427,14 +427,14 @@ impl<'a> TerminalReader<MemoryTerminal> for MemoryReadGuard<'a> {
-> io::Result<()> { Ok(()) }

unsafe fn prepare_with_lock(&mut self,
_lock: &mut TerminalWriter<MemoryTerminal>,
_lock: &mut dyn TerminalWriter<MemoryTerminal>,
_block_signals: bool, _report_signals: SignalSet)
-> io::Result<()> { Ok(()) }

fn restore(&mut self, _state: ()) -> io::Result<()> { Ok(()) }

unsafe fn restore_with_lock(&mut self,
_lock: &mut TerminalWriter<MemoryTerminal>, _state: ())
_lock: &mut dyn TerminalWriter<MemoryTerminal>, _state: ())
-> io::Result<()> { Ok(()) }

fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead> {
Expand Down
2 changes: 1 addition & 1 deletion src/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<'a, 'b: 'a, Term: 'b + Terminal> Prompter<'a, 'b, Term> {
self.read.bindings.find(seq).cloned()
}

fn get_function(&self, name: &str) -> Option<&Arc<Function<Term>>> {
fn get_function(&self, name: &str) -> Option<&Arc<dyn Function<Term>>> {
self.read.functions.get(name)
}

Expand Down
26 changes: 13 additions & 13 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) struct Read<Term: Terminal> {
pub macro_buffer: String,

pub bindings: SequenceMap<Cow<'static, str>, Command>,
pub functions: HashMap<Cow<'static, str>, Arc<Function<Term>>>,
pub functions: HashMap<Cow<'static, str>, Arc<dyn Function<Term>>>,

/// Current input sequence
pub sequence: String,
Expand All @@ -81,7 +81,7 @@ pub(crate) struct Read<Term: Terminal> {
pub overwritten_chars: String,

/// Configured completer
pub completer: Arc<Completer<Term>>,
pub completer: Arc<dyn Completer<Term>>,
/// Character appended to completions
pub completion_append_character: Option<char>,
/// Current set of possible completions
Expand Down Expand Up @@ -113,7 +113,7 @@ pub(crate) struct Read<Term: Terminal> {
}

pub(crate) struct ReadLock<'a, Term: 'a + Terminal> {
term: Box<TerminalReader<Term> + 'a>,
term: Box<dyn TerminalReader<Term> + 'a>,
data: MutexGuard<'a, Read<Term>>,
}

Expand Down Expand Up @@ -450,13 +450,13 @@ impl<'a, Term: 'a + Terminal> Reader<'a, Term> {
}

/// Returns a reference to the current completer instance.
pub fn completer(&self) -> &Arc<Completer<Term>> {
pub fn completer(&self) -> &Arc<dyn Completer<Term>> {
&self.lock.completer
}

/// Replaces the current completer, returning the previous instance.
pub fn set_completer(&mut self, completer: Arc<Completer<Term>>)
-> Arc<Completer<Term>> {
pub fn set_completer(&mut self, completer: Arc<dyn Completer<Term>>)
-> Arc<dyn Completer<Term>> {
replace(&mut self.lock.completer, completer)
}

Expand Down Expand Up @@ -698,15 +698,15 @@ impl<'a, Term: 'a + Terminal> Reader<'a, Term> {
/// this is not a requirement.
///
/// Returns the function previously defined with the same name.
pub fn define_function<T>(&mut self, name: T, cmd: Arc<Function<Term>>)
-> Option<Arc<Function<Term>>> where T: Into<Cow<'static, str>> {
pub fn define_function<T>(&mut self, name: T, cmd: Arc<dyn Function<Term>>)
-> Option<Arc<dyn Function<Term>>> where T: Into<Cow<'static, str>> {
self.lock.define_function(name, cmd)
}

/// Removes a function defined with the given name.
///
/// Returns the defined function.
pub fn remove_function(&mut self, name: &str) -> Option<Arc<Function<Term>>> {
pub fn remove_function(&mut self, name: &str) -> Option<Arc<dyn Function<Term>>> {
self.lock.remove_function(name)
}

Expand Down Expand Up @@ -734,7 +734,7 @@ impl<'a, Term: 'a + Terminal> Reader<'a, Term> {
}

impl<'a, Term: 'a + Terminal> ReadLock<'a, Term> {
pub fn new(term: Box<TerminalReader<Term> + 'a>, data: MutexGuard<'a, Read<Term>>)
pub fn new(term: Box<dyn TerminalReader<Term> + 'a>, data: MutexGuard<'a, Read<Term>>)
-> ReadLock<'a, Term> {
ReadLock{term, data}
}
Expand Down Expand Up @@ -950,12 +950,12 @@ impl<Term: Terminal> Read<Term> {
.map(|(_, cmd)| cmd)
}

pub fn define_function<T>(&mut self, name: T, cmd: Arc<Function<Term>>)
-> Option<Arc<Function<Term>>> where T: Into<Cow<'static, str>> {
pub fn define_function<T>(&mut self, name: T, cmd: Arc<dyn Function<Term>>)
-> Option<Arc<dyn Function<Term>>> where T: Into<Cow<'static, str>> {
self.functions.insert(name.into(), cmd)
}

pub fn remove_function(&mut self, name: &str) -> Option<Arc<Function<Term>>> {
pub fn remove_function(&mut self, name: &str) -> Option<Arc<dyn Function<Term>>> {
self.functions.remove(name)
}

Expand Down
18 changes: 9 additions & 9 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ pub trait Terminal: Sized + Send + Sync {
/// that lock and granting access to such operations.
///
/// The lock must not be released until the returned value is dropped.
fn lock_read<'a>(&'a self) -> Box<TerminalReader<Self> + 'a>;
fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a>;

/// Acquires a lock on terminal write operations and returns a value holding
/// that lock and granting access to such operations.
///
/// The lock must not be released until the returned value is dropped.
fn lock_write<'a>(&'a self) -> Box<TerminalWriter<Self> + 'a>;
fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a>;
}

/// Holds a lock on `Terminal` read operations
Expand Down Expand Up @@ -75,7 +75,7 @@ pub trait TerminalReader<Term: Terminal> {
///
/// This method must be called with a `TerminalWriter` instance returned
/// by the same `Terminal` instance to which this `TerminalReader` belongs.
unsafe fn prepare_with_lock(&mut self, lock: &mut TerminalWriter<Term>,
unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>,
block_signals: bool, report_signals: SignalSet)
-> io::Result<Term::PrepareState>;

Expand All @@ -88,7 +88,7 @@ pub trait TerminalReader<Term: Terminal> {
///
/// This method must be called with a `TerminalWriter` instance returned
/// by the same `Terminal` instance to which this `TerminalReader` belongs.
unsafe fn restore_with_lock(&mut self, lock: &mut TerminalWriter<Term>,
unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>,
state: Term::PrepareState) -> io::Result<()>;

/// Reads some input from the terminal and appends it to the given buffer.
Expand Down Expand Up @@ -170,7 +170,7 @@ impl DefaultTerminal {
mortal::Terminal::stderr().map(DefaultTerminal)
}

unsafe fn cast_writer<'a>(writer: &'a mut TerminalWriter<Self>)
unsafe fn cast_writer<'a>(writer: &'a mut dyn TerminalWriter<Self>)
-> &'a mut TerminalWriteGuard<'a> {
&mut *(writer as *mut _ as *mut TerminalWriteGuard)
}
Expand All @@ -183,11 +183,11 @@ impl Terminal for DefaultTerminal {
self.0.name()
}

fn lock_read<'a>(&'a self) -> Box<TerminalReader<Self> + 'a> {
fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a> {
Box::new(self.0.lock_read().unwrap())
}

fn lock_write<'a>(&'a self) -> Box<TerminalWriter<Self> + 'a> {
fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a> {
Box::new(self.0.lock_write().unwrap())
}
}
Expand All @@ -205,7 +205,7 @@ impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> {
}

unsafe fn prepare_with_lock(&mut self,
lock: &mut TerminalWriter<DefaultTerminal>,
lock: &mut dyn TerminalWriter<DefaultTerminal>,
block_signals: bool, report_signals: SignalSet)
-> io::Result<PrepareState> {
let lock = DefaultTerminal::cast_writer(lock);
Expand All @@ -224,7 +224,7 @@ impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> {
}

unsafe fn restore_with_lock(&mut self,
lock: &mut TerminalWriter<DefaultTerminal>, state: PrepareState)
lock: &mut dyn TerminalWriter<DefaultTerminal>, state: PrepareState)
-> io::Result<()> {
let lock = DefaultTerminal::cast_writer(lock);
self.restore_with_lock(lock, state)
Expand Down
4 changes: 2 additions & 2 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ pub(crate) struct Write {
}

pub(crate) struct WriteLock<'a, Term: 'a + Terminal> {
term: Box<TerminalWriter<Term> + 'a>,
term: Box<dyn TerminalWriter<Term> + 'a>,
data: MutexGuard<'a, Write>,
}

impl<'a, Term: Terminal> WriteLock<'a, Term> {
pub fn new(term: Box<TerminalWriter<Term> + 'a>, data: MutexGuard<'a, Write>)
pub fn new(term: Box<dyn TerminalWriter<Term> + 'a>, data: MutexGuard<'a, Write>)
-> WriteLock<'a, Term> {
WriteLock{term, data}
}
Expand Down