Skip to content

Commit a5c900f

Browse files
committed
Generate accessors for enum variants.
1 parent a6e054a commit a5c900f

19 files changed

Lines changed: 802 additions & 134 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/enums/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generated.h
2+
generated.rs
3+
generated.cpp

examples/enums/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "example-enums"
3+
version = "0.10.0"
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
license.workspace = true
7+
publish = false
8+
9+
[lib]
10+
crate-type = ["staticlib"]
11+
12+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13+
14+
[dependencies]

examples/enums/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
a.out: main.cpp generated.h src/generated.rs src/lib.rs ../../target/release/libexample_enums.a
2+
${CXX} -std=c++17 -Werror main.cpp -g -L ../../target/release/ -l example_enums
3+
4+
../../target/release/libexample_enums.a:
5+
cargo build --release
6+
7+
generated.h ./src/generated.rs: main.zng
8+
cd ../../zngur-cli && cargo run g -i ../examples/enums/main.zng --crate-name "crate"
9+
10+
.PHONY: ../../target/release/libexample_enums.a generated.h clean
11+
12+
clean:
13+
rm -f generated.h src/generated.rs a.out actual_output.txt

examples/enums/NMakefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CXX = cl.exe
2+
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20
3+
WINLIBS = ntdll.lib
4+
5+
EXAMPLE_NAME = simple
6+
7+
GENERATED = generated.h src/generated.rs
8+
9+
RUSTLIB_PATH = ../../target/release/
10+
11+
RUSTLIB = example_$(EXAMPLE_NAME).lib
12+
13+
a.exe : main.cpp src/lib.rs $(GENERATED) $(RUSTLIB_PATH)/$(RUSTLIB)
14+
$(CXX) $(CXXFLAGS) main.cpp generated.cpp /Fe:a.exe /link $(WINLIBS) $(RUSTLIB) /LIBPATH:$(RUSTLIB_PATH)
15+
16+
$(RUSTLIB_PATH)/$(RUSTLIB) :
17+
cargo build --release
18+
19+
$(GENERATED) : main.zng
20+
cd ../../zngur-cli && cargo run g -i ../examples/$(EXAMPLE_NAME)/main.zng --crate-name "crate"
21+
22+
clean :
23+
- del /f /q generated.h generated.cpp src\generated.rs a.exe main.obj generated.obj actual_output.txt 2>nul

examples/enums/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Example: Simple
2+
3+
A simple example, used as a demo in the main README file.
4+
5+
To run this example:
6+
7+
```
8+
make
9+
./a.out
10+
```

examples/enums/expected_output.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
17
2+
s[2] = 7
3+
4+
thread panicked at examples/simple/src/generated.rs:351:40:
5+
called `Option::unwrap()` on a `None` value
6+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
7+
s[4] = Rust panic happened
8+
hello 2 2
9+
hello 5 7
10+
hello 7 14
11+
hello 3 17
12+
34 17
13+
vector iterator has been destructed
14+
[main.cpp:71] t = [
15+
10,
16+
20,
17+
60,
18+
]

examples/enums/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <cstdio>
2+
#include <iostream>
3+
#include <vector>
4+
5+
#include "./generated.h"
6+
7+
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
8+
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
9+
10+
template<class T> using Option = rust::std::option::Option<T>;
11+
12+
int main() {
13+
Option<int> v = Option<int>::Some(42);
14+
std::visit(overloaded{
15+
[](Option<int>::None) { std::cout << "none" << std::endl; },
16+
[](Option<int>::Some s) { std::cout << "some " << s.f0 << std::endl; },
17+
}, v.match());
18+
19+
if (auto r = Option<int>::Some::match_mut(v)) {
20+
*rust::RefMut(r->f0) = 67;
21+
}
22+
23+
if (auto r = Option<int>::Some::match(v)) {
24+
std::cout << r->f0 << "!!" << std::endl;
25+
}
26+
}

examples/enums/main.zng

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#convert_panic_to_exception
2+
3+
mod ::std {
4+
type option::Option<i32> {
5+
#layout(size = 8, align = 4);
6+
wellknown_traits(Debug, Copy);
7+
8+
variant None {}
9+
variant Some {
10+
field 0 (offset = auto, type = i32);
11+
}
12+
}
13+
}

examples/enums/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[rustfmt::skip]
2+
mod generated;

0 commit comments

Comments
 (0)