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

Make enums PartialEq to their repr type #159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/Language/Rust/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ data ItemKind
| Use String
| Enum String [Enumerator]
| CloneImpl Type -- TODO: generalize `impl` syntax
| PartialEqImpl Type Type
deriving Show

instance Pretty ItemKind where
Expand Down Expand Up @@ -169,6 +170,18 @@ instance Pretty ItemKind where
, text "}"
]) $+$
text "}"
pPrint (PartialEqImpl ty oty) =
hsep [text "impl", text "PartialEq<", pPrint oty, text ">", text "for", pPrint ty] <+> text "{" $+$
nest 4 (hsep
[ text "fn"
, text "eq" <> parens (text "&self, other: &" <> pPrint oty)
, text "->"
, text "bool"
, text "{"
, nest 4 (text "*self as " <> pPrint ty <> text "== *other")
, text "}"
]) $+$
text "}"

data ExternItem
= ExternFn String [(Var, Type)] Bool Type
Expand Down
4 changes: 3 additions & 1 deletion src/Language/Rust/Corrode/C.md
Original file line number Diff line number Diff line change
Expand Up @@ -3744,8 +3744,10 @@ above.
let attrs = [ Rust.Attribute "derive(Clone, Copy)"
, Rust.Attribute (concat [ "repr(", repr, ")" ])
]
let impl = Rust.Item
when shouldEmit $
emitItems [Rust.Item attrs Rust.Public (Rust.Enum name enums)]
emitItems [Rust.Item attrs Rust.Public (Rust.Enum name enums),
Rust.Item [] Rust.Private (Rust.PartialEqImpl (Rust.TypeName name) (Rust.TypeName repr))]
return (IsEnum name)

forM_ items $ \ (ident, _mexpr) -> addSymbolIdentAction ident $ do
Expand Down