├── .gitattributes ├── .github └── workflows │ └── zig.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── build.zig └── src └── main.zig /.gitattributes: -------------------------------------------------------------------------------- 1 | *.zig text eol=lf -------------------------------------------------------------------------------- /.github/workflows/zig.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: Zig 4 | 5 | # Controls when the action will run. Triggers the workflow on push or pull request 6 | # events but only for the master branch 7 | on: 8 | #schedule: 9 | # - cron: '0 18 * * *' 10 | push: 11 | branches: [ master ] 12 | pull_request: 13 | 14 | jobs: 15 | test: 16 | strategy: 17 | matrix: 18 | os: [ubuntu-latest, macos-latest, windows-latest] 19 | runs-on: ${{matrix.os}} 20 | steps: 21 | - uses: actions/checkout@v3 22 | - uses: goto-bus-stop/setup-zig@v2 23 | with: 24 | version: master 25 | - run: zig build test 26 | lint: 27 | runs-on: ubuntu-latest 28 | steps: 29 | - uses: actions/checkout@v3 30 | - uses: goto-bus-stop/setup-zig@v2 31 | with: 32 | version: master 33 | - run: zig fmt --ast-check --check **.zig 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | zig-* 2 | docs/ -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at . All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 DutchGhost 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zorrow 2 | This is a userlevel implementation of borrowchk in Zig. 3 | 4 | This system is *not* borrowchk, as it requires to pass in unique types for operations 5 | like acquiring a borrow, and reading from it. This library does not check for uniqueness 6 | of the types passed in, it's up to the programmer to do this correctly. 7 | An example of what is ment by `unique type`: 8 | 9 | ```Zig 10 | var cell = RefCell(usize, opaque {}).init(10); 11 | 12 | var borrow = cell.borrow(opaque {}); 13 | defer borrow.release(); 14 | 15 | var value = borrow.read(opaque {}); 16 | ``` 17 | Here we see `opaque {}` three times. it is required to pass those in, as Zorrow 18 | heavily relies on unique types passed into it's API. 19 | 20 | ## Minimum supported `Zig` 21 | `master` 22 | 23 | ## Recent changes 24 | * 0.2.1 25 | * Change all occurrences of `var` as argument type to `anytype`. 26 | * 0.2 27 | * Allow the use of `.{}` where previously `struct {}` was required. 28 | * 0.1 29 | * Initial implementation 30 | 31 | ## Contributors 32 | * [suirad](https://github.com/suirad) 33 | * [kprotty](https://github.com/kprotty) 34 | * [Kassane](https://github.com/kassane) 35 | -------------------------------------------------------------------------------- /build.zig: -------------------------------------------------------------------------------- 1 | const Builder = @import("std").build.Builder; 2 | 3 | pub fn build(b: *Builder) void { 4 | const mode = b.standardReleaseOptions(); 5 | const lib = b.addStaticLibrary("zorrow", "src/main.zig"); 6 | lib.setBuildMode(mode); 7 | lib.install(); 8 | 9 | var main_tests = b.addTest("src/main.zig"); 10 | main_tests.setBuildMode(mode); 11 | 12 | const test_step = b.step("test", "Run library tests"); 13 | test_step.dependOn(&main_tests.step); 14 | } 15 | -------------------------------------------------------------------------------- /src/main.zig: -------------------------------------------------------------------------------- 1 | const testing = @import("std").testing; 2 | 3 | fn Borrow(comptime T: type, comptime borrows: *usize) type { 4 | comptime var alive = true; 5 | 6 | return struct { 7 | pointer: *const T, 8 | const Self = @This(); 9 | pub fn read(self: *const Self, comptime uniq: anytype) T { 10 | _ = uniq; 11 | if (!alive) 12 | @compileError("Borrow no longer alive!"); 13 | 14 | return self.pointer.*; 15 | } 16 | 17 | pub fn release(self: Self) void { 18 | _ = self; 19 | alive = false; 20 | borrows.* -= 1; 21 | } 22 | }; 23 | } 24 | 25 | fn BorrowMut(comptime T: type, comptime borrowmuts: ?*usize) type { 26 | _ = borrowmuts; 27 | comptime var alive: bool = true; 28 | 29 | return struct { 30 | pointer: *T, 31 | 32 | const Self = @This(); 33 | 34 | pub fn write(self: *Self, value: T, comptime uniq: anytype) void { 35 | _ = uniq; 36 | if (!alive) 37 | @compileError("BorrowMut no longer alive!"); 38 | 39 | self.pointer.* = value; 40 | } 41 | 42 | pub fn read(self: *const Self, comptime uniq: anytype) T { 43 | _ = uniq; 44 | if (!alive) 45 | @compileError("BorrowMut no longer alive!"); 46 | 47 | return self.pointer.*; 48 | } 49 | 50 | pub fn release(self: Self) void { 51 | _ = self; 52 | alive = false; 53 | // borrowmuts.?.* -= 1; 54 | } 55 | }; 56 | } 57 | 58 | /// A borrowable memory location. 59 | /// Borrows are checked at compiletime. It works just like 60 | /// a read-write lock; There may be many borrows at a time, 61 | /// *or* only one mutable borrow at a time. 62 | pub fn RefCell(comptime T: type, comptime _: anytype) type { 63 | comptime var borrows: usize = 0; 64 | comptime var mutborrows: usize = 0; 65 | 66 | return struct { 67 | value: T, 68 | 69 | const Self = @This(); 70 | pub fn init(value: T) Self { 71 | return Self{ .value = value }; 72 | } 73 | 74 | /// Borrows the value. As long as a `borrow` is alive, there may not be 75 | /// any mutable borrow alive. Borrows can be released by calling `.release()`. 76 | pub fn borrow(self: *const Self, comptime uniq: anytype) Borrow(T, &borrows) { 77 | _ = uniq; 78 | comptime if (borrows > 0 and mutborrows > 0) { 79 | @compileError("Value has already been unwrapped!"); 80 | } else if (mutborrows > 0) { 81 | @compileError("There is a mutable borrow active!"); 82 | }; 83 | 84 | borrows += 1; 85 | 86 | return .{ .pointer = &self.value }; 87 | } 88 | 89 | /// Borrows the value mutably. As long as `mut borrow` is alive, there may not be 90 | /// any other borrow or mutable borrow alive. In order words, a live mutable borrow 91 | /// is a unique borrow. 92 | pub fn borrowMut(self: *Self, comptime uniq: anytype) BorrowMut(T, &mutborrows) { 93 | _ = uniq; 94 | comptime if (borrows > 0 and mutborrows > 0) { 95 | @compileError("Value has already been unwrapped!"); 96 | } else if (borrows > 0 or mutborrows > 0) { 97 | @compileError("There is a borrow[mut] active!"); 98 | }; 99 | 100 | mutborrows += 1; 101 | 102 | return .{ .pointer = &self.value }; 103 | } 104 | 105 | pub fn unwrap(self: *Self, comptime uniq: anytype) T { 106 | _ = uniq; 107 | comptime if (borrows > 0 and mutborrows > 0) { 108 | @compileError("Value has already been unwrapped!"); 109 | } else if (borrows > 0 or mutborrows > 0) { 110 | @compileError("There is an borrow[mut] active!"); 111 | }; 112 | 113 | mutborrows += 1; 114 | borrows += 1; 115 | return self.value; 116 | } 117 | }; 118 | } 119 | 120 | test "unwrap" { 121 | var cell = RefCell(usize, opaque {}).init(10); 122 | var cell2 = RefCell(usize, opaque {}).init(10); 123 | 124 | try testing.expectEqual(cell.unwrap(opaque {}), cell2.unwrap(opaque {})); 125 | //_ = cell.unwrap(opaque {}); // <--- FAILS: already unwrapped 126 | //_ = cell.borrow(opaque {}); // <--- FAILS: already unwrapped 127 | //_ = cell.borrowMut(opaque {}); // <--- FAILS: already unwrapped 128 | } 129 | 130 | test "borrowck" { 131 | var cell = RefCell(usize, opaque {}).init(10); 132 | var b0 = cell.borrow(opaque {}); 133 | var b1 = cell.borrow(opaque {}); 134 | 135 | try testing.expectEqual(b0.read(opaque {}), 10); 136 | try testing.expectEqual(b1.read(opaque {}), 10); 137 | 138 | b0.release(); 139 | // _ = b0.read(opaque {}); // <--- FAILS: read after release 140 | _ = b1.read(opaque {}); 141 | _ = b1.read(opaque {}); 142 | b1.release(); 143 | 144 | var bm1 = cell.borrowMut(opaque {}); 145 | // var b2 = cell.borrow(opaque {}); // <--- FAILS: borrow while mut borrow is active 146 | // var bm2 = cell.borrowMut(opaque {}); // <--- FAILS borrowmut while mut borrow is active 147 | bm1.write(11, opaque {}); 148 | try testing.expectEqual(bm1.read(opaque {}), 11); 149 | bm1.release(); 150 | // bm1.write(20, opaque {}); // <--- FAILS: write after release 151 | } 152 | 153 | test "defer release" { 154 | var cell = RefCell(usize, opaque {}).init(20); 155 | { 156 | var borrow = cell.borrow(opaque {}); 157 | defer borrow.release(); 158 | 159 | try testing.expectEqual(borrow.read(opaque {}), 20); 160 | } 161 | // fixme: Borrow no longer alive! 162 | // { 163 | // var mutborrow = cell.borrowMut(opaque {}); 164 | // defer mutborrow.release(); 165 | 166 | // try testing.expectEqual(mutborrow.read(opaque {}), 20); 167 | 168 | // mutborrow.write(0, opaque {}); 169 | // try testing.expectEqual(mutborrow.read(opaque {}), 0); 170 | // } 171 | } 172 | 173 | test "Rcursively references all the declarations" { 174 | testing.refAllDeclsRecursive(@This()); 175 | } 176 | --------------------------------------------------------------------------------