Skip to content

Display and Error traits are clunky #21576

@andrewrk

Description

@andrewrk

here's a diff I just made:

-impl std::error::Error for CreationError {
-    fn description(&self) -> &str {
+impl CreationError {
+    fn to_string(&self) -> &str {
         match self {
             &CreationError::OsError(ref text) => text.as_slice(),
             &CreationError::NotSupported => "Some of the requested attributes are not supported",
@@ -82,6 +82,18 @@ impl std::error::Error for CreationError {
     }
 }

+impl std::fmt::Display for CreationError {
+    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+        formatter.write_str(self.to_string())
+    }
+}
+
+impl std::error::Error for CreationError {
+    fn description(&self) -> &str {
+        self.to_string()
+    }
+}
+

Display::fmt cannot call Error::description because Error trait requires Display trait. So I end up creating yet another method, and the other two call it. Pretty clunky when I just wanted a single to_string-like method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions