diff --git a/src/backend/glutin/mod.rs b/src/backend/glutin/mod.rs index 533a2041084..7c4a88a0250 100644 --- a/src/backend/glutin/mod.rs +++ b/src/backend/glutin/mod.rs @@ -188,19 +188,14 @@ impl Display { impl fmt::Display for DisplayCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(fmt, "{}", self.description()) + match self { + DisplayCreationError::GlutinCreationError(err) => write!(fmt, "{}", err), + DisplayCreationError::IncompatibleOpenGl(err) => write!(fmt, "{}", err), + } } } impl Error for DisplayCreationError { - #[inline] - fn description(&self) -> &str { - match *self { - DisplayCreationError::GlutinCreationError(ref err) => err.description(), - DisplayCreationError::IncompatibleOpenGl(ref err) => err.description(), - } - } - #[inline] fn source(&self) -> Option<&(dyn Error + 'static)> { match *self { diff --git a/src/buffer/alloc.rs b/src/buffer/alloc.rs index af5f658a497..c410769c915 100644 --- a/src/buffer/alloc.rs +++ b/src/buffer/alloc.rs @@ -32,20 +32,17 @@ pub enum ReadError { impl fmt::Display for ReadError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for ReadError { - fn description(&self) -> &str { use self::ReadError::*; - match *self { + let desc = match *self { NotSupported => "The backend doesn't support reading from a buffer", ContextLost => "The context has been lost. Reading from the buffer would return garbage data", - } + }; + fmt.write_str(desc) } } +impl Error for ReadError {} + /// Error that can happen when copying data between buffers. #[derive(Debug, Copy, Clone)] pub enum CopyError { @@ -55,19 +52,16 @@ pub enum CopyError { impl fmt::Display for CopyError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for CopyError { - fn description(&self) -> &str { use self::CopyError::*; - match *self { + let desc = match *self { NotSupported => "The backend doesn't support copying between buffers", - } + }; + fmt.write_str(desc) } } +impl Error for CopyError {} + /// A buffer in the graphics card's memory. pub struct Alloc { context: Rc, diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 03921dc984c..35180b1a723 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -192,18 +192,15 @@ pub enum BufferCreationError { impl fmt::Display for BufferCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) + let desc = match self { + BufferCreationError::OutOfMemory => "Not enough memory to create the buffer", + BufferCreationError::BufferTypeNotSupported => "This type of buffer is not supported", + }; + fmt.write_str(desc) } } -impl Error for BufferCreationError { - fn description(&self) -> &str { - match self { - &BufferCreationError::OutOfMemory => "Not enough memory to create the buffer", - &BufferCreationError::BufferTypeNotSupported => "This type of buffer is not supported", - } - } -} +impl Error for BufferCreationError {} /// How the buffer is created. #[derive(Debug, Copy, Clone, PartialEq, Eq)] diff --git a/src/draw_parameters/query.rs b/src/draw_parameters/query.rs index 28c377e22f2..3658599a04d 100644 --- a/src/draw_parameters/query.rs +++ b/src/draw_parameters/query.rs @@ -67,19 +67,16 @@ pub enum QueryCreationError { impl fmt::Display for QueryCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for QueryCreationError { - fn description(&self) -> &str { use self::QueryCreationError::*; - match *self { + let desc = match *self { NotSupported => "The given query type is not supported", - } + }; + fmt.write_str(desc) } } +impl Error for QueryCreationError {} + /// Error that can happen when writing the value of a query to a buffer. #[derive(Copy, Clone, Debug)] pub enum ToBufferError { @@ -89,19 +86,16 @@ pub enum ToBufferError { impl fmt::Display for ToBufferError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for ToBufferError { - fn description(&self) -> &str { use self::ToBufferError::*; - match *self { + let desc = match *self { NotSupported => "Writing the result to a buffer is not supported", - } + }; + fmt.write_str(desc) } } +impl Error for ToBufferError {} + impl RawQuery { /// Builds a new query. Returns `None` if the backend doesn't support this type. pub fn new(facade: &F, ty: QueryType) -> Result diff --git a/src/fbo.rs b/src/fbo.rs index dd474b3b09e..d5117944f18 100644 --- a/src/fbo.rs +++ b/src/fbo.rs @@ -603,19 +603,7 @@ pub enum ValidationError { impl fmt::Display for ValidationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { use self::ValidationError::*; - match *self { - TooManyColorAttachments{ ref maximum, ref obtained } => - write!(fmt, "{}: found {}, maximum: {}", self.description(), obtained, maximum), - _ => - write!(fmt, "{}", self.description()), - } - } -} - -impl Error for ValidationError { - fn description(&self) -> &str { - use self::ValidationError::*; - match *self { + let desc = match self { EmptyFramebufferObjectsNotSupported => "You requested an empty framebuffer object, but they are not supported", EmptyFramebufferUnsupportedDimensions => @@ -626,10 +614,18 @@ impl Error for ValidationError { "All attachments must have the same number of samples", TooManyColorAttachments {..} => "Backends only support a certain number of color attachments", + }; + match self { + TooManyColorAttachments{ ref maximum, ref obtained } => + write!(fmt, "{}: found {}, maximum: {}", desc, obtained, maximum), + _ => + fmt.write_str(desc), } } } +impl Error for ValidationError {} + /// Data structure stored in the hashmap. /// /// These attachments are guaranteed to be valid. diff --git a/src/framebuffer/render_buffer.rs b/src/framebuffer/render_buffer.rs index 0fc5ed33448..96f01f27274 100644 --- a/src/framebuffer/render_buffer.rs +++ b/src/framebuffer/render_buffer.rs @@ -38,19 +38,16 @@ pub enum CreationError { impl fmt::Display for CreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for CreationError { - fn description(&self) -> &str { use self::CreationError::*; - match *self { + let desc = match *self { FormatNotSupported => "The requested format is not supported", - } + }; + fmt.write_str(desc) } } +impl Error for CreationError {} + impl From for CreationError { fn from(_: image_format::FormatNotSupportedError) -> CreationError { CreationError::FormatNotSupported diff --git a/src/image_format.rs b/src/image_format.rs index 34664e7ee7b..5e4bc35159d 100644 --- a/src/image_format.rs +++ b/src/image_format.rs @@ -18,15 +18,11 @@ pub struct FormatNotSupportedError; impl fmt::Display for FormatNotSupportedError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) + write!(fmt, "Format is not supported by OpenGL") } } -impl Error for FormatNotSupportedError { - fn description(&self) -> &str { - "Format is not supported by OpenGL" - } -} +impl Error for FormatNotSupportedError {} /// Texture format request. #[derive(Copy, Clone, Debug)] diff --git a/src/index/buffer.rs b/src/index/buffer.rs index c818ca2e26e..34819944415 100644 --- a/src/index/buffer.rs +++ b/src/index/buffer.rs @@ -30,23 +30,20 @@ pub enum CreationError { impl fmt::Display for CreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for CreationError { - fn description(&self) -> &str { use self::CreationError::*; - match *self { + let desc = match *self { IndexTypeNotSupported => "The type of index is not supported by the backend", PrimitiveTypeNotSupported => "The type of primitives is not supported by the backend", BufferCreationError(_) => "An error happened while creating the buffer", - } + }; + fmt.write_str(desc) } +} +impl Error for CreationError { fn source(&self) -> Option<&(dyn Error + 'static)> { use self::CreationError::*; match *self { diff --git a/src/lib.rs b/src/lib.rs index 4864f7a1655..16fd3c61380 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -956,9 +956,20 @@ pub enum DrawError { } impl Error for DrawError { - fn description(&self) -> &str { + fn source(&self) -> Option<&(dyn Error + 'static)> { use self::DrawError::*; match *self { + UniformBlockLayoutMismatch { ref err, .. } => Some(err), + _ => None, + } + } +} + + +impl fmt::Display for DrawError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use self::DrawError::*; + let desc = match self { NoDepthBuffer => "A depth function has been requested but no depth buffer is available", AttributeTypeMismatch => @@ -1013,55 +1024,40 @@ impl Error for DrawError { "Restarting indices (multiple objects per draw call) is not supported by the backend", ClipPlaneIndexOutOfBounds => "Tried to enable a clip plane that does not exist." - } - } - - fn source(&self) -> Option<&(dyn Error + 'static)> { - use self::DrawError::*; - match *self { - UniformBlockLayoutMismatch { ref err, .. } => Some(err), - _ => None, - } - } -} - - -impl fmt::Display for DrawError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use self::DrawError::*; - match *self { - UniformTypeMismatch { ref name, ref expected } => + }; + match self { + UniformTypeMismatch { name, expected } => write!( fmt, "{}, got: {:?}, expected: {:?}", - self.description(), + desc, name, expected, ), - UniformBufferToValue { ref name } => + UniformBufferToValue { name } => write!( fmt, "{}: {}", - self.description(), + desc, name, ), - UniformValueToBlock { ref name } => + UniformValueToBlock { name } => write!( fmt, "{}: {}", - self.description(), + desc, name, ), - UniformBlockLayoutMismatch { ref name, ref err } => + UniformBlockLayoutMismatch { name, err } => write!( fmt, "{}: {}, caused by {}", - self.description(), + desc, name, err, ), _ => - write!(fmt, "{}", self.description()), + fmt.write_str(desc), } } } @@ -1087,21 +1083,18 @@ pub enum SwapBuffersError { AlreadySwapped, } -impl Error for SwapBuffersError { - fn description(&self) -> &str { +impl Error for SwapBuffersError {} + +impl fmt::Display for SwapBuffersError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { use self::SwapBuffersError::*; - match *self { + let desc = match *self { ContextLost => "the OpenGL context has been lost and needs to be recreated", AlreadySwapped => "the buffers have already been swapped", - } - } -} - -impl fmt::Display for SwapBuffersError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(fmt, "{}", self.description()) + }; + fmt.write_str(desc) } } @@ -1256,16 +1249,11 @@ pub struct IncompatibleOpenGl(pub String); impl fmt::Display for IncompatibleOpenGl { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) + fmt.write_str("The OpenGL implementation is too old to work with glium") } } -impl Error for IncompatibleOpenGl { - #[inline] - fn description(&self) -> &str { - "The OpenGL implementation is too old to work with glium" - } -} +impl Error for IncompatibleOpenGl {} #[allow(dead_code)] #[inline] diff --git a/src/ops/read.rs b/src/ops/read.rs index 028366661e9..184f716b187 100644 --- a/src/ops/read.rs +++ b/src/ops/read.rs @@ -78,24 +78,21 @@ pub enum ReadError { impl fmt::Display for ReadError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for ReadError { - fn description(&self) -> &str { use self::ReadError::*; - match *self { + let desc = match *self { OutputFormatNotSupported => "The implementation doesn't support converting to the requested output format", AttachmentTypeNotSupported => "The implementation doesn't support reading a depth, depth-stencil or stencil attachment", ClampingNotSupported => "Clamping the values is not supported by the implementation", - } + }; + fmt.write_str(desc) } } +impl Error for ReadError {} + /// Reads pixels from the source into the destination. /// /// Panics if the destination is not large enough. diff --git a/src/program/mod.rs b/src/program/mod.rs index 8dcd9aa5b96..b405dfb85b6 100644 --- a/src/program/mod.rs +++ b/src/program/mod.rs @@ -140,21 +140,7 @@ pub enum ProgramCreationError { impl fmt::Display for ProgramCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { use self::ProgramCreationError::*; - match *self { - CompilationError(ref s, _) => - write!(fmt, "{}: {}", self.description(), s), - LinkingError(ref s) => - write!(fmt, "{}: {}", self.description(), s), - _ => - write!(fmt, "{}", self.description()), - } - } -} - -impl Error for ProgramCreationError { - fn description(&self) -> &str { - use self::ProgramCreationError::*; - match *self { + let desc = match *self { CompilationError(_,typ) => { match typ { ShaderType::Vertex => "Compilation error in vertex shader", @@ -177,10 +163,20 @@ impl Error for ProgramCreationError { "Point size is not supported by the backend.", BinaryHeaderError => "The glium-specific binary header was not found or is corrupt.", + }; + match *self { + CompilationError(ref s, _) => + write!(fmt, "{}: {}", desc, s), + LinkingError(ref s) => + write!(fmt, "{}: {}", desc, s), + _ => + write!(fmt, "{}", desc), } } } +impl Error for ProgramCreationError {} + /// Error type that is returned by the `program!` macro. #[derive(Clone, Debug)] pub enum ProgramChooserCreationError { @@ -194,20 +190,15 @@ pub enum ProgramChooserCreationError { impl fmt::Display for ProgramChooserCreationError { #[inline] fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(fmt, "{}", self.description()) - } -} - -impl Error for ProgramChooserCreationError { - #[inline] - fn description(&self) -> &str { use self::ProgramChooserCreationError::*; match *self { - ProgramCreationError(ref err) => err.description(), - NoVersion => "No version of the program has been found for the current OpenGL version.", + ProgramCreationError(ref err) => write!(fmt, "{}", err), + NoVersion => fmt.write_str("No version of the program has been found for the current OpenGL version."), } } +} +impl Error for ProgramChooserCreationError { #[inline] fn source(&self) -> Option<&(dyn Error + 'static)> { use self::ProgramChooserCreationError::*; @@ -235,20 +226,17 @@ pub enum GetBinaryError { impl fmt::Display for GetBinaryError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for GetBinaryError { - fn description(&self) -> &str { use self::GetBinaryError::*; - match *self { + let desc = match *self { NotSupported => "The backend doesn't support binary", NoFormats => "The backend does not supply any binary formats.", - } + }; + fmt.write_str(desc) } } +impl Error for GetBinaryError {} + /// Input when creating a program. pub enum ProgramCreationInput<'a> { /// Use GLSL source code. diff --git a/src/texture/buffer_texture.rs b/src/texture/buffer_texture.rs index 8254876db77..46bbf0224f5 100644 --- a/src/texture/buffer_texture.rs +++ b/src/texture/buffer_texture.rs @@ -94,24 +94,21 @@ pub enum TextureCreationError { impl fmt::Display for TextureCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for TextureCreationError { - fn description(&self) -> &str { use self::TextureCreationError::*; - match *self { + let desc = match *self { NotSupported => "Buffer textures are not supported at all", FormatNotSupported => "The requested format is not supported in combination with the given texture buffer type", TooLarge => "The size of the buffer that you are trying to bind exceeds `GL_MAX_TEXTURE_BUFFER_SIZE`", - } + }; + fmt.write_str(desc) } } +impl Error for TextureCreationError {} + /// Error that can happen while building a buffer texture. #[derive(Copy, Clone, Debug)] pub enum CreationError { @@ -124,21 +121,18 @@ pub enum CreationError { impl fmt::Display for CreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for CreationError { - fn description(&self) -> &str { use self::CreationError::*; - match *self { + let desc = match *self { BufferCreationError(_) => "Failed to create the buffer", TextureCreationError(_) => "Failed to create the texture", - } + }; + fmt.write_str(desc) } +} +impl Error for CreationError { fn source(&self) -> Option<&(dyn Error + 'static)> { use self::CreationError::*; match *self { diff --git a/src/texture/get_format.rs b/src/texture/get_format.rs index ec9332f5a6c..0f30670913f 100644 --- a/src/texture/get_format.rs +++ b/src/texture/get_format.rs @@ -19,20 +19,16 @@ pub enum GetFormatError { impl fmt::Display for GetFormatError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for GetFormatError { - fn description(&self) -> &str { use self::GetFormatError::*; - match *self { - NotSupported => - "The backend doesn't support retrieving the internal format", - } + let desc = match self { + NotSupported => "The backend doesn't support retrieving the internal format", + }; + fmt.write_str(desc) } } +impl Error for GetFormatError {} + /// Internal format of a texture. /// /// The actual format of a texture is not necessarily one of the predefined ones, so we have diff --git a/src/texture/mod.rs b/src/texture/mod.rs index 195fd7d562d..d594977d09f 100644 --- a/src/texture/mod.rs +++ b/src/texture/mod.rs @@ -805,24 +805,21 @@ pub enum TextureCreationError { impl fmt::Display for TextureCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for TextureCreationError { - fn description(&self) -> &str { use self::TextureCreationError::*; - match *self { + let desc = match *self { FormatNotSupported => "The requested format is not supported by the backend", DimensionsNotSupported => "The requested texture dimensions are not supported", TypeNotSupported => "The texture format is not supported by the backend", - } + }; + fmt.write_str(desc) } } +impl Error for TextureCreationError {} + impl From for TextureCreationError { #[inline] fn from(_: FormatNotSupportedError) -> TextureCreationError { diff --git a/src/uniforms/mod.rs b/src/uniforms/mod.rs index 956b763dd94..1b376f7060c 100644 --- a/src/uniforms/mod.rs +++ b/src/uniforms/mod.rs @@ -213,22 +213,6 @@ pub enum LayoutMismatchError { } impl Error for LayoutMismatchError { - fn description(&self) -> &str { - use self::LayoutMismatchError::*; - match *self { - TypeMismatch { .. } => - "There is a mismatch in the type of one element", - LayoutMismatch { .. } => - "The expected layout is totally different from what we have", - OffsetMismatch { .. } => - "The type of data is good, but there is a misalignment", - MemberMismatch { .. } => - "There is a mismatch in a submember of this layout", - MissingField { .. } => - "A field is missing in either the expected of the input data layout", - } - } - fn source(&self) -> Option<&(dyn Error + 'static)> { use self::LayoutMismatchError::*; match *self { @@ -241,13 +225,25 @@ impl Error for LayoutMismatchError { impl fmt::Display for LayoutMismatchError { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { use self::LayoutMismatchError::*; + let desc = match *self { + TypeMismatch { .. } => + "There is a mismatch in the type of one element", + LayoutMismatch { .. } => + "The expected layout is totally different from what we have", + OffsetMismatch { .. } => + "The type of data is good, but there is a misalignment", + MemberMismatch { .. } => + "There is a mismatch in a submember of this layout", + MissingField { .. } => + "A field is missing in either the expected of the input data layout", + }; match *self { //duplicate Patternmatching, different Types can't be condensed TypeMismatch { ref expected, ref obtained } => write!( fmt, "{}, got: {:?}, expected: {:?}", - self.description(), + desc, obtained, expected, ), @@ -255,7 +251,7 @@ impl fmt::Display for LayoutMismatchError { write!( fmt, "{}, got: {:?}, expected: {:?}", - self.description(), + desc, obtained, expected, ), @@ -263,7 +259,7 @@ impl fmt::Display for LayoutMismatchError { write!( fmt, "{}, got: {}, expected: {}", - self.description(), + desc, obtained, expected, ), @@ -271,7 +267,7 @@ impl fmt::Display for LayoutMismatchError { write!( fmt, "{}, {}: {}", - self.description(), + desc, member, err, ), @@ -279,7 +275,7 @@ impl fmt::Display for LayoutMismatchError { write!( fmt, "{}: {}", - self.description(), + desc, name, ), } diff --git a/src/vertex/buffer.rs b/src/vertex/buffer.rs index e4ab98c0333..d2b72ebc021 100644 --- a/src/vertex/buffer.rs +++ b/src/vertex/buffer.rs @@ -35,19 +35,16 @@ impl From for CreationError { impl fmt::Display for CreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for CreationError { - fn description(&self) -> &str { use self::CreationError::*; - match *self { + let desc = match self { FormatNotSupported => "The vertex format is not supported by the backend", BufferCreationError(_) => "Error while creating the vertex buffer", - } + }; + fmt.write_str(desc) } +} +impl Error for CreationError { fn source(&self) -> Option<&(dyn Error + 'static)> { use self::CreationError::*; match *self { diff --git a/src/vertex/transform_feedback.rs b/src/vertex/transform_feedback.rs index 33de5ffc0e7..6af19120253 100644 --- a/src/vertex/transform_feedback.rs +++ b/src/vertex/transform_feedback.rs @@ -108,22 +108,19 @@ pub enum TransformFeedbackSessionCreationError { impl fmt::Display for TransformFeedbackSessionCreationError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.description()) - } -} - -impl Error for TransformFeedbackSessionCreationError { - fn description(&self) -> &str { use self::TransformFeedbackSessionCreationError::*; - match *self { + let desc = match *self { NotSupported => "Transform feedback is not supported by the OpenGL implementation", WrongVertexFormat => "The format of the output doesn't match what the program is expected to output", - } + }; + fmt.write_str(desc) } } +impl Error for TransformFeedbackSessionCreationError {} + /// Returns true if transform feedback is supported by the OpenGL implementation. #[inline] pub fn is_transform_feedback_supported(facade: &F) -> bool where F: Facade {