Replace deprecated Error methods

This commit is contained in:
sterlingjensen 2020-01-02 18:17:22 -06:00 committed by Christian Duerr
parent d774c7f3a3
commit 05df4f4dba
6 changed files with 103 additions and 153 deletions

View File

@ -1,4 +1,6 @@
use std::env; use std::env;
use std::fmt::{self, Display, Formatter};
use std::fs;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
@ -24,7 +26,7 @@ use crate::config::ui_config::UIConfig;
pub type Config = TermConfig<UIConfig>; pub type Config = TermConfig<UIConfig>;
/// Result from config loading /// Result from config loading
pub type Result<T> = ::std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// Errors occurring during config loading /// Errors occurring during config loading
#[derive(Debug)] #[derive(Debug)]
@ -43,46 +45,37 @@ pub enum Error {
} }
impl std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn (::std::error::Error)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
Error::NotFound => None, Error::NotFound => None,
Error::ReadingEnvHome(ref err) => Some(err), Error::ReadingEnvHome(err) => err.source(),
Error::Io(ref err) => Some(err), Error::Io(err) => err.source(),
Error::Yaml(ref err) => Some(err), Error::Yaml(err) => err.source(),
}
}
fn description(&self) -> &str {
match *self {
Error::NotFound => "Couldn't locate config file",
Error::ReadingEnvHome(ref err) => err.description(),
Error::Io(ref err) => err.description(),
Error::Yaml(ref err) => err.description(),
} }
} }
} }
impl std::fmt::Display for Error { impl Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match self {
Error::NotFound => write!(f, "{}", ::std::error::Error::description(self)), Error::NotFound => write!(f, "Couldn't locate config file"),
Error::ReadingEnvHome(ref err) => { Error::ReadingEnvHome(err) => {
write!(f, "Couldn't read $HOME environment variable: {}", err) write!(f, "Couldn't read $HOME environment variable: {}", err)
}, },
Error::Io(ref err) => write!(f, "Error reading config file: {}", err), Error::Io(err) => write!(f, "Error reading config file: {}", err),
Error::Yaml(ref err) => write!(f, "Problem with config: {}", err), Error::Yaml(err) => write!(f, "Problem with config: {}", err),
} }
} }
} }
impl From<env::VarError> for Error { impl From<env::VarError> for Error {
fn from(val: env::VarError) -> Error { fn from(val: env::VarError) -> Self {
Error::ReadingEnvHome(val) Error::ReadingEnvHome(val)
} }
} }
impl From<io::Error> for Error { impl From<io::Error> for Error {
fn from(val: io::Error) -> Error { fn from(val: io::Error) -> Self {
if val.kind() == io::ErrorKind::NotFound { if val.kind() == io::ErrorKind::NotFound {
Error::NotFound Error::NotFound
} else { } else {
@ -92,7 +85,7 @@ impl From<io::Error> for Error {
} }
impl From<serde_yaml::Error> for Error { impl From<serde_yaml::Error> for Error {
fn from(val: serde_yaml::Error) -> Error { fn from(val: serde_yaml::Error) -> Self {
Error::Yaml(val) Error::Yaml(val)
} }
} }
@ -154,7 +147,7 @@ pub fn reload_from(path: &PathBuf) -> Result<Config> {
} }
fn read_config(path: &PathBuf) -> Result<Config> { fn read_config(path: &PathBuf) -> Result<Config> {
let mut contents = std::fs::read_to_string(path)?; let mut contents = fs::read_to_string(path)?;
// Remove UTF-8 BOM // Remove UTF-8 BOM
if contents.chars().nth(0) == Some('\u{FEFF}') { if contents.chars().nth(0) == Some('\u{FEFF}') {
@ -165,10 +158,10 @@ fn read_config(path: &PathBuf) -> Result<Config> {
} }
fn parse_config(contents: &str) -> Result<Config> { fn parse_config(contents: &str) -> Result<Config> {
match serde_yaml::from_str(&contents) { match serde_yaml::from_str(contents) {
Err(error) => { Err(error) => {
// Prevent parsing error with an empty string and commented out file. // Prevent parsing error with an empty string and commented out file.
if std::error::Error::description(&error) == "EOF while parsing a value" { if error.to_string() == "EOF while parsing a value" {
Ok(Config::default()) Ok(Config::default())
} else { } else {
Err(Error::Yaml(error)) Err(Error::Yaml(error))

View File

@ -15,7 +15,7 @@
//! The display subsystem including window management, font rasterization, and //! The display subsystem including window management, font rasterization, and
//! GPU drawing. //! GPU drawing.
use std::f64; use std::f64;
use std::fmt; use std::fmt::{self, Formatter};
use std::time::Instant; use std::time::Instant;
use glutin::dpi::{PhysicalPosition, PhysicalSize}; use glutin::dpi::{PhysicalPosition, PhysicalSize};
@ -61,56 +61,47 @@ pub enum Error {
} }
impl std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn (std::error::Error)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
Error::Window(ref err) => Some(err), Error::Window(err) => err.source(),
Error::Font(ref err) => Some(err), Error::Font(err) => err.source(),
Error::Render(ref err) => Some(err), Error::Render(err) => err.source(),
Error::ContextError(ref err) => Some(err), Error::ContextError(err) => err.source(),
}
}
fn description(&self) -> &str {
match *self {
Error::Window(ref err) => err.description(),
Error::Font(ref err) => err.description(),
Error::Render(ref err) => err.description(),
Error::ContextError(ref err) => err.description(),
} }
} }
} }
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match self {
Error::Window(ref err) => err.fmt(f), Error::Window(err) => err.fmt(f),
Error::Font(ref err) => err.fmt(f), Error::Font(err) => err.fmt(f),
Error::Render(ref err) => err.fmt(f), Error::Render(err) => err.fmt(f),
Error::ContextError(ref err) => err.fmt(f), Error::ContextError(err) => err.fmt(f),
} }
} }
} }
impl From<window::Error> for Error { impl From<window::Error> for Error {
fn from(val: window::Error) -> Error { fn from(val: window::Error) -> Self {
Error::Window(val) Error::Window(val)
} }
} }
impl From<font::Error> for Error { impl From<font::Error> for Error {
fn from(val: font::Error) -> Error { fn from(val: font::Error) -> Self {
Error::Font(val) Error::Font(val)
} }
} }
impl From<renderer::Error> for Error { impl From<renderer::Error> for Error {
fn from(val: renderer::Error) -> Error { fn from(val: renderer::Error) -> Self {
Error::Render(val) Error::Render(val)
} }
} }
impl From<glutin::ContextError> for Error { impl From<glutin::ContextError> for Error {
fn from(val: glutin::ContextError) -> Error { fn from(val: glutin::ContextError) -> Self {
Error::ContextError(val) Error::ContextError(val)
} }
} }
@ -241,7 +232,7 @@ impl Display {
_ => (), _ => (),
} }
Ok(Display { Ok(Self {
window, window,
renderer, renderer,
glyph_cache, glyph_cache,

View File

@ -38,6 +38,7 @@ use alacritty_terminal::term::cell::{self, Flags};
use alacritty_terminal::term::color::Rgb; use alacritty_terminal::term::color::Rgb;
use alacritty_terminal::term::{self, CursorKey, RenderableCell, RenderableCellContent, SizeInfo}; use alacritty_terminal::term::{self, CursorKey, RenderableCell, RenderableCellContent, SizeInfo};
use alacritty_terminal::util; use alacritty_terminal::util;
use std::fmt::{self, Display, Formatter};
pub mod rects; pub mod rects;
@ -77,32 +78,22 @@ pub enum Error {
ShaderCreation(ShaderCreationError), ShaderCreation(ShaderCreationError),
} }
impl ::std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn (::std::error::Error)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
Error::ShaderCreation(ref err) => Some(err), Error::ShaderCreation(err) => err.source(),
}
}
fn description(&self) -> &str {
match *self {
Error::ShaderCreation(ref err) => err.description(),
} }
} }
} }
impl ::std::fmt::Display for Error { impl Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { write!(f, "There was an error initializing the shaders: {}", self)
Error::ShaderCreation(ref err) => {
write!(f, "There was an error initializing the shaders: {}", err)
},
}
} }
} }
impl From<ShaderCreationError> for Error { impl From<ShaderCreationError> for Error {
fn from(val: ShaderCreationError) -> Error { fn from(val: ShaderCreationError) -> Self {
Error::ShaderCreation(val) Error::ShaderCreation(val)
} }
} }
@ -184,7 +175,7 @@ pub struct GlyphCache {
/// glyph offset /// glyph offset
glyph_offset: Delta<i8>, glyph_offset: Delta<i8>,
metrics: ::font::Metrics, metrics: font::Metrics,
} }
impl GlyphCache { impl GlyphCache {
@ -205,7 +196,7 @@ impl GlyphCache {
let metrics = rasterizer.metrics(regular, font.size)?; let metrics = rasterizer.metrics(regular, font.size)?;
let mut cache = GlyphCache { let mut cache = Self {
cache: HashMap::default(), cache: HashMap::default(),
cursor_cache: HashMap::default(), cursor_cache: HashMap::default(),
rasterizer, rasterizer,
@ -288,7 +279,7 @@ impl GlyphCache {
FontDesc::new(desc.family.clone(), style) FontDesc::new(desc.family.clone(), style)
} }
pub fn get<'a, L>(&'a mut self, glyph_key: GlyphKey, loader: &mut L) -> &'a Glyph pub fn get<L>(&mut self, glyph_key: GlyphKey, loader: &mut L) -> &Glyph
where where
L: LoadGlyph, L: LoadGlyph,
{ {
@ -461,8 +452,8 @@ pub struct Batch {
impl Batch { impl Batch {
#[inline] #[inline]
pub fn new() -> Batch { pub fn new() -> Self {
Batch { tex: 0, instances: Vec::with_capacity(BATCH_MAX) } Self { tex: 0, instances: Vec::with_capacity(BATCH_MAX) }
} }
pub fn add_item(&mut self, mut cell: RenderableCell, glyph: &Glyph) { pub fn add_item(&mut self, mut cell: RenderableCell, glyph: &Glyph) {
@ -668,7 +659,7 @@ impl QuadRenderer {
if cfg!(feature = "live-shader-reload") { if cfg!(feature = "live-shader-reload") {
util::thread::spawn_named("live shader reload", move || { util::thread::spawn_named("live shader reload", move || {
let (tx, rx) = ::std::sync::mpsc::channel(); let (tx, rx) = std::sync::mpsc::channel();
// The Duration argument is a debouncing period. // The Duration argument is a debouncing period.
let mut watcher = let mut watcher =
watcher(tx, Duration::from_millis(10)).expect("create file watcher"); watcher(tx, Duration::from_millis(10)).expect("create file watcher");
@ -695,7 +686,7 @@ impl QuadRenderer {
}); });
} }
let mut renderer = QuadRenderer { let mut renderer = Self {
program, program,
rect_program, rect_program,
vao, vao,
@ -1028,7 +1019,7 @@ impl<'a, C> RenderApi<'a, C> {
cursor_key.is_wide, cursor_key.is_wide,
)) ))
}); });
self.add_render_item(cell, &glyph); self.add_render_item(cell, glyph);
return; return;
}, },
RenderableCellContent::Chars(chars) => chars, RenderableCellContent::Chars(chars) => chars,
@ -1195,7 +1186,7 @@ impl TextShaderProgram {
assert_uniform_valid!(projection, cell_dim, background); assert_uniform_valid!(projection, cell_dim, background);
let shader = TextShaderProgram { let shader = Self {
id: program, id: program,
u_projection: projection, u_projection: projection,
u_cell_dim: cell_dim, u_cell_dim: cell_dim,
@ -1273,7 +1264,7 @@ impl RectShaderProgram {
// get uniform locations // get uniform locations
let u_color = unsafe { gl::GetUniformLocation(program, b"color\0".as_ptr() as *const _) }; let u_color = unsafe { gl::GetUniformLocation(program, b"color\0".as_ptr() as *const _) };
let shader = RectShaderProgram { id: program, u_color }; let shader = Self { id: program, u_color };
unsafe { gl::UseProgram(0) } unsafe { gl::UseProgram(0) }
@ -1427,37 +1418,29 @@ pub enum ShaderCreationError {
Link(String), Link(String),
} }
impl ::std::error::Error for ShaderCreationError { impl std::error::Error for ShaderCreationError {
fn cause(&self) -> Option<&dyn (::std::error::Error)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
ShaderCreationError::Io(ref err) => Some(err), ShaderCreationError::Io(err) => err.source(),
_ => None, _ => None,
} }
} }
fn description(&self) -> &str {
match *self {
ShaderCreationError::Io(ref err) => err.description(),
ShaderCreationError::Compile(ref _path, ref s) => s.as_str(),
ShaderCreationError::Link(ref s) => s.as_str(),
}
}
} }
impl ::std::fmt::Display for ShaderCreationError { impl Display for ShaderCreationError {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match self {
ShaderCreationError::Io(ref err) => write!(f, "Couldn't read shader: {}", err), ShaderCreationError::Io(err) => write!(f, "Couldn't read shader: {}", err),
ShaderCreationError::Compile(ref path, ref log) => { ShaderCreationError::Compile(path, log) => {
write!(f, "Failed compiling shader at {}: {}", path.display(), log) write!(f, "Failed compiling shader at {}: {}", path.display(), log)
}, },
ShaderCreationError::Link(ref log) => write!(f, "Failed linking shader: {}", log), ShaderCreationError::Link(log) => write!(f, "Failed linking shader: {}", log),
} }
} }
} }
impl From<io::Error> for ShaderCreationError { impl From<io::Error> for ShaderCreationError {
fn from(val: io::Error) -> ShaderCreationError { fn from(val: io::Error) -> Self {
ShaderCreationError::Io(val) ShaderCreationError::Io(val)
} }
} }
@ -1516,7 +1499,7 @@ enum AtlasInsertError {
} }
impl Atlas { impl Atlas {
fn new(size: i32) -> Atlas { fn new(size: i32) -> Self {
let mut id: GLuint = 0; let mut id: GLuint = 0;
unsafe { unsafe {
gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1); gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1);
@ -1542,7 +1525,7 @@ impl Atlas {
gl::BindTexture(gl::TEXTURE_2D, 0); gl::BindTexture(gl::TEXTURE_2D, 0);
} }
Atlas { id, width: size, height: size, row_extent: 0, row_baseline: 0, row_tallest: 0 } Self { id, width: size, height: size, row_extent: 0, row_baseline: 0, row_tallest: 0 }
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {

View File

@ -14,7 +14,7 @@
use std::convert::From; use std::convert::From;
#[cfg(not(any(target_os = "macos", windows)))] #[cfg(not(any(target_os = "macos", windows)))]
use std::ffi::c_void; use std::ffi::c_void;
use std::fmt; use std::fmt::{self, Display, Formatter};
#[cfg(not(any(target_os = "macos", windows)))] #[cfg(not(any(target_os = "macos", windows)))]
use std::os::raw::c_ulong; use std::os::raw::c_ulong;
@ -61,50 +61,42 @@ pub enum Error {
} }
/// Result of fallible operations concerning a Window. /// Result of fallible operations concerning a Window.
type Result<T> = ::std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
impl ::std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn (::std::error::Error)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
Error::ContextCreation(ref err) => Some(err), Error::ContextCreation(err) => err.source(),
Error::Context(ref err) => Some(err), Error::Context(err) => err.source(),
Error::Font(ref err) => Some(err), Error::Font(err) => err.source(),
}
}
fn description(&self) -> &str {
match *self {
Error::ContextCreation(ref _err) => "Error creating gl context",
Error::Context(ref _err) => "Error operating on render context",
Error::Font(ref err) => err.description(),
} }
} }
} }
impl fmt::Display for Error { impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match self {
Error::ContextCreation(ref err) => write!(f, "Error creating GL context; {}", err), Error::ContextCreation(err) => write!(f, "Error creating GL context; {}", err),
Error::Context(ref err) => write!(f, "Error operating on render context; {}", err), Error::Context(err) => write!(f, "Error operating on render context; {}", err),
Error::Font(ref err) => err.fmt(f), Error::Font(err) => err.fmt(f),
} }
} }
} }
impl From<glutin::CreationError> for Error { impl From<glutin::CreationError> for Error {
fn from(val: glutin::CreationError) -> Error { fn from(val: glutin::CreationError) -> Self {
Error::ContextCreation(val) Error::ContextCreation(val)
} }
} }
impl From<glutin::ContextError> for Error { impl From<glutin::ContextError> for Error {
fn from(val: glutin::ContextError) -> Error { fn from(val: glutin::ContextError) -> Self {
Error::Context(val) Error::Context(val)
} }
} }
impl From<font::Error> for Error { impl From<font::Error> for Error {
fn from(val: font::Error) -> Error { fn from(val: font::Error) -> Self {
Error::Font(val) Error::Font(val)
} }
} }
@ -126,7 +118,7 @@ fn create_gl_window(
.build_windowed(window, event_loop)?; .build_windowed(window, event_loop)?;
// Make the context current so OpenGL operations can run // Make the context current so OpenGL operations can run
let windowed_context = unsafe { windowed_context.make_current().map_err(|(_, e)| e)? }; let windowed_context = unsafe { windowed_context.make_current().map_err(|(_, err)| err)? };
Ok(windowed_context) Ok(windowed_context)
} }
@ -171,7 +163,7 @@ impl Window {
} }
} }
Ok(Window { current_mouse_cursor, mouse_visible: true, windowed_context }) Ok(Self { current_mouse_cursor, mouse_visible: true, windowed_context })
} }
pub fn set_inner_size(&mut self, size: LogicalSize) { pub fn set_inner_size(&mut self, size: LogicalSize) {

View File

@ -15,7 +15,7 @@
//! Rasterization powered by FreeType and FontConfig //! Rasterization powered by FreeType and FontConfig
use std::cmp::{min, Ordering}; use std::cmp::{min, Ordering};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt::{self, Display, Formatter};
use std::path::PathBuf; use std::path::PathBuf;
use freetype::freetype_sys; use freetype::freetype_sys;
@ -46,7 +46,7 @@ struct Face {
} }
impl fmt::Debug for Face { impl fmt::Debug for Face {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Face") f.debug_struct("Face")
.field("ft_face", &self.ft_face) .field("ft_face", &self.ft_face)
.field("key", &self.key) .field("key", &self.key)
@ -668,32 +668,23 @@ pub enum Error {
} }
impl std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match self {
Error::FreeType(ref err) => Some(err), Error::FreeType(err) => err.source(),
_ => None, _ => None,
} }
} }
fn description(&self) -> &str {
match *self {
Error::FreeType(ref err) => err.description(),
Error::MissingFont(ref _desc) => "Couldn't find the requested font",
Error::FontNotLoaded => "Tried to operate on font that hasn't been loaded",
Error::MissingSizeMetrics => "Tried to get size metrics from a face without a size",
}
}
} }
impl std::fmt::Display for Error { impl Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self { match self {
Error::FreeType(ref err) => err.fmt(f), Error::FreeType(err) => err.fmt(f),
Error::MissingFont(ref desc) => write!( Error::MissingFont(err) => write!(
f, f,
"Couldn't find a font with {}\n\tPlease check the font config in your \ "Couldn't find a font with {}\n\tPlease check the font config in your \
alacritty.yml.", alacritty.yml.",
desc err
), ),
Error::FontNotLoaded => f.write_str("Tried to use a font that hasn't been loaded"), Error::FontNotLoaded => f.write_str("Tried to use a font that hasn't been loaded"),
Error::MissingSizeMetrics => { Error::MissingSizeMetrics => {

View File

@ -83,8 +83,8 @@ impl Display for Error {
} }
impl std::error::Error for Error { impl std::error::Error for Error {
fn description(&self) -> &str { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
&self.message None
} }
} }