Commit f87e62bd authored by Kenton Varda's avatar Kenton Varda

Don't document inlines.

parent 7a537646
......@@ -117,7 +117,6 @@ struct Person {
Fields can have default values:
{% highlight capnp %}
foo @0 :Int32 = 123;
bar @1 :Text = "blah";
......@@ -126,7 +125,6 @@ qux @3 :Person = (name = "Bob", email = "bob@example.com");
corge @4 :Void = void;
{% endhighlight %}
### Unions
A union is two or more fields of a struct which are stored in the same location. Only one of
......@@ -161,6 +159,11 @@ Notes:
for the `unemployed` or `selfEmployed` cases, but we still want the union to distinguish these
states from others.
### Dynamically-typed Fields
A struct may have a field with type `Object`. This field's value can be of any pointer type -- i.e.
any struct, interface, list, or blob. This is essentially like a `void*` in C.
### Enums
An enum is a type with a small finite set of symbolic values.
......@@ -414,69 +417,6 @@ are possible, but unlikely -- there would have to be on the order of a billion t
becomes a real concern. Collisions from misuse (e.g. copying an example without changing the ID)
are much more likely.
## Advanced Topics
### Dynamically-typed Fields
A struct may have a field with type `Object`. This field's value can be of any pointer type -- i.e.
any struct, interface, list, or blob. This is essentially like a `void*` in C.
### Inlining Structs
Say you have a small struct which you know will never add new fields. For efficiency, you may want
instance of this struct to be "inlined" into larger structs where it is used. This saves eight
bytes of space per usage (the size of a pointer) and may improve cache locality.
To inline a struct, you must first declare that it has fixed-width, and specify the sizes of its
data and pointer sections:
{% highlight capnp %}
struct Point16 fixed(4 bytes) {
x @0 :UInt16;
y @1 :UInt16;
}
struct Name fixed(2 pointers) {
first @0 :Text;
last @1 :Text;
}
struct TextWithHash fixed(8 bytes, 1 pointers) {
hash @0 :UInt64;
text @1 :Text;
}
{% endhighlight %}
The compiler will produce an error if the specified size is too small to hold the defined fields,
so if you are unsure how much space you need, simply delcare your struct `fixed()` and the compiler
will tell you.
Once you have a fixed-width struct, you must explicitly declare it `Inline` at the usage site:
{% highlight capnp %}
struct Foo {
a @0 :Point16; # NOT inlined
b @1 :Inline(Point16); # inlined!
}
{% endhighlight %}
### Inlining Lists and Data
You may also inline fixed-length lists and data.
{% highlight capnp %}
struct Foo {
sha1Hash @0 :InlineData(20); # 160-bit fixed-width.
vertex3 @1 :InlineList(Float32, 3); # x, y, and z coordinates.
vertexList @2 :List(InlineList(Float32, 3));
# Much more efficient than List(List(Float32))!
}
{% endhighlight %}
At this time, there is no `InlineText` because text almost always has variable length.
## Evolving Your Protocol
A protocol can be changed in the following ways without breaking backwards-compatibility:
......@@ -489,14 +429,10 @@ A protocol can be changed in the following ways without breaking backwards-compa
parameter list and must have default values.
* Any symbolic name can be changed, as long as the ordinal numbers stay the same.
* Types definitions can be moved to different scopes.
* A field of type `List(T)`, where `T` is a primitive type, non-inline blob, or
non-inline list, may be changed to type `List(U)`, where `U` is a struct type whose `@0` field is
of type `T`. This rule is useful when you realize too late that you need to attach some extra
data to each element of your list. Without this rule, you would be stuck defining parallel
lists, which are ugly and error-prone.
* A struct that is not already `fixed` can be made `fixed`. However, once a struct is declared
`fixed`, the declaration cannot be removed or changed, as this would change the layout of `Inline`
uses of the struct.
* A field of type `List(T)`, where `T` is a primitive type, blob, or list, may be changed to type
`List(U)`, where `U` is a struct type whose `@0` field is of type `T`. This rule is useful when
you realize too late that you need to attach some extra data to each element of your list.
Without this rule, you would be stuck defining parallel lists, which are ugly and error-prone.
Any other change should be assumed NOT to be safe. Also, these rules only apply to the Cap'n Proto
native encoding. It is sometimes useful to transcode Cap'n Proto types to other formats, like
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment