Skip to content

Commit

Permalink
Add sharp for layout decl to make it consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 6, 2023
1 parent 4f2d7a5 commit 570ce70
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions book/src/call_rust_from_cpp/layout_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using Zngur with explicit data layout annotations for unstable types is usually

These are the layout policies Zngur currently supports:

## `layout(size = X, align = Y)`
## `#layout(size = X, align = Y)`

This is the normal mode, which you should provide exact value of size and align for your type, which enables storing
them by value in the stack of C++. This mode has the best performance, but it has problems mentioned above.
Expand All @@ -34,7 +34,7 @@ size and align, such as:
- `[T; N]` where `T` has stable layout
- primitives

## `layout_conservative(size = X, align = Y)`
## `#layout_conservative(size = X, align = Y)`

{{#include ../unimplemented_begin.md}}1{{#include ../unimplemented_end.md}}

Expand Down
18 changes: 9 additions & 9 deletions book/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Copy it into `src/lib.rs`. Now we need to declare things that we need to access

```
type crate::Inventory {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
fn new_empty(u32) -> crate::Inventory;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ To ensure that everything works, let's add a `#[derive(Debug)]` to `Inventory` a

```
type crate::Inventory {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
wellknown_traits(Debug);
fn new_empty(u32) -> crate::Inventory;
Expand Down Expand Up @@ -144,11 +144,11 @@ impl Inventory {

```
type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
}
type crate::Inventory {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
wellknown_traits(Debug);
fn new_empty(u32) -> crate::Inventory;
Expand Down Expand Up @@ -195,7 +195,7 @@ of that function:
// ...
type crate::Item {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
}
type crate::Inventory {
Expand All @@ -209,11 +209,11 @@ need to add the constructor for the `Item`:

```
type ::std::string::String {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);
}
type crate::Item {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
constructor { name: ::std::string::String, size: u32 };
}
Expand All @@ -231,7 +231,7 @@ type str {
```

There are some new things here. First, since `str` is a primitive it doesn't need full path. Then there is `wellknown_traits(?Sized)` instead
of `layout(size = X, align = Y)` which tells Zngur that this type is unsized and it should consider its references as fat and prevent storing it
of `#layout(size = X, align = Y)` which tells Zngur that this type is unsized and it should consider its references as fat and prevent storing it
by value.

Now you may wonder how we can obtain a `&str` to make a `String` from it? Fortunately, Zngur has some special support for primitive types and it
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Inventory {

```
type ::std::vec::Vec<crate::Item> {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);
wellknown_traits(Debug);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/cxx_demo/main.zng
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

Expand All @@ -19,13 +19,13 @@ type ::core::primitive::str {

mod crate {
type MultiBuf {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

fn next_chunk(&mut self) -> &[u8];
}

type BlobMetadata {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

fn default() -> BlobMetadata;
fn set_size(&mut self, usize);
Expand All @@ -39,7 +39,7 @@ mod crate {
}

type Box<dyn BlobStoreTrait> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);
}
}

Expand Down
14 changes: 7 additions & 7 deletions examples/memory_management/main.zng
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#convert_panic_to_exception

type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

type bool {
layout(size = 1, align = 1);
#layout(size = 1, align = 1);
wellknown_traits(Copy);
}

Expand All @@ -16,13 +16,13 @@ type str {

mod crate {
type PrintOnDropPair {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

constructor { first: PrintOnDrop, second: PrintOnDrop };
}

type PrintOnDrop {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);
constructor(&str);

fn clone(&self) -> PrintOnDrop;
Expand All @@ -39,7 +39,7 @@ mod crate {
}

type Box<dyn PrintOnDropConsumer> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

fn deref_mut(&mut self) -> &mut dyn PrintOnDropConsumer use ::std::ops::DerefMut;
}
Expand All @@ -55,15 +55,15 @@ mod crate {
mod ::std {
mod option {
type Option<&crate::PrintOnDrop> {
layout(size = 8, align = 8);
#layout(size = 8, align = 8);

fn unwrap(self) -> &crate::PrintOnDrop;
}
}

mod vec {
type Vec<crate::PrintOnDrop> {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);

fn new() -> Vec<crate::PrintOnDrop>;
fn push(&mut self, crate::PrintOnDrop);
Expand Down
12 changes: 6 additions & 6 deletions examples/osmium/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"

type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

Expand All @@ -21,21 +21,21 @@ type str {
}

type ::std::option::Option<&str> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

constructor None;
constructor Some(&str);
}

type ::std::string::String {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);

fn new() -> ::std::string::String;
fn push_str(&mut self, &str);
}

type crate::Reader {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

constructor(ZngurCppOpaqueOwnedObject);

Expand All @@ -59,13 +59,13 @@ type crate::Node {
}

type crate::BendHandler {
layout(size = 8, align = 8);
#layout(size = 8, align = 8);

fn way(&mut self, &crate::Way) use crate::Handler;
}

type crate::Flags {
layout(size = 1, align = 1);
#layout(size = 1, align = 1);

fn bits(&self) -> u8;
}
Expand Down
14 changes: 7 additions & 7 deletions examples/rayon/main.zng
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

type bool {
layout(size = 1, align = 1);
#layout(size = 1, align = 1);
wellknown_traits(Copy);
}

type ::std::option::Option<&u64> {
layout(size = 8, align = 8);
#layout(size = 8, align = 8);
wellknown_traits(Debug, Copy);

fn unwrap(self) -> &u64;
}

type Box<dyn Fn(&u64) -> bool + Sync + Send> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);
}

type ::rayon::iter::Filter<::rayon::iter::Copied<::rayon::slice::Iter<u64>>, Box<dyn Fn(&u64) -> bool + Sync + Send>> {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

fn count(self) -> usize use ::rayon::iter::ParallelIterator;
}

type ::rayon::slice::Iter<u64> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

fn sum<u64>(self) -> u64 use ::rayon::iter::ParallelIterator;
fn copied<u64>(self) -> ::rayon::iter::Copied<::rayon::slice::Iter<u64>> use ::rayon::iter::ParallelIterator;
}

type ::rayon::iter::Copied<::rayon::slice::Iter<u64>> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

fn filter<Box<dyn Fn(&u64) -> bool + Sync + Send>>(self, Box<dyn Fn(&u64) -> bool + Sync + Send>)
-> ::rayon::iter::Filter<::rayon::iter::Copied<::rayon::slice::Iter<u64>>, Box<dyn Fn(&u64) -> bool + Sync + Send>>
Expand Down
4 changes: 2 additions & 2 deletions examples/rustyline/main.zng
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This example uses various layout policies to demonstrate them. See https://hkalbasi.github.io/zngur/call_rust_from_cpp/layout_policy.html

type () {
layout(size = 0, align = 1); // Unit type has stable layout
#layout(size = 0, align = 1); // Unit type has stable layout
wellknown_traits(Copy);
}

Expand All @@ -13,7 +13,7 @@ type str {
}

type bool {
layout(size = 1, align = 1); // primitives like bool have stable layout
#layout(size = 1, align = 1); // primitives like bool have stable layout
wellknown_traits(Copy);
}

Expand Down
16 changes: 8 additions & 8 deletions examples/simple/main.zng
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#convert_panic_to_exception

type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

type Box<dyn Fn(i32) -> i32> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);
}

mod ::std {
type option::Option<i32> {
layout(size = 8, align = 4);
#layout(size = 8, align = 4);
wellknown_traits(Copy);

constructor None;
Expand All @@ -21,29 +21,29 @@ mod ::std {
}

type option::Option<&i32> {
layout(size = 8, align = 8);
#layout(size = 8, align = 8);
wellknown_traits(Copy);

fn unwrap(self) -> &i32;
}

type iter::Map<::std::vec::IntoIter<i32>, Box<dyn Fn(i32) -> i32>> {
layout(size = 48, align = 8);
#layout(size = 48, align = 8);

fn sum<i32>(self) -> i32;
}

mod vec {
type IntoIter<i32> {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

fn sum<i32>(self) -> i32;
fn map<i32, Box<dyn Fn(i32) -> i32>>(self, Box<dyn Fn(i32) -> i32>)
-> ::std::iter::Map<::std::vec::IntoIter<i32>, Box<dyn Fn(i32) -> i32>>;
}

type Vec<i32> {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);
wellknown_traits(Debug);

fn new() -> Vec<i32>;
Expand All @@ -60,7 +60,7 @@ mod ::std {
}

type Box<dyn ::std::iter::Iterator<Item = i32>> {
layout(size = 16, align = 8);
#layout(size = 16, align = 8);

fn collect<::std::vec::Vec<i32>>(self) -> ::std::vec::Vec<i32>;
}
10 changes: 5 additions & 5 deletions examples/tutorial/main.zng
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
type () {
layout(size = 0, align = 1);
#layout(size = 0, align = 1);
wellknown_traits(Copy);
}

type ::std::vec::Vec<crate::Item> {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);
wellknown_traits(Debug);
}

Expand All @@ -15,17 +15,17 @@ type str {
}

type ::std::string::String {
layout(size = 24, align = 8);
#layout(size = 24, align = 8);
}

type crate::Item {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);

constructor { name: ::std::string::String, size: u32 };
}

type crate::Inventory {
layout(size = 32, align = 8);
#layout(size = 32, align = 8);
wellknown_traits(Debug);

fn new_empty(u32) -> crate::Inventory;
Expand Down
Loading

0 comments on commit 570ce70

Please sign in to comment.