mirror of
https://github.com/bspeice/speice.io
synced 2024-11-14 22:18:10 -05:00
1 line
18 KiB
JavaScript
1 line
18 KiB
JavaScript
"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["832"],{8603:function(e,t,n){n.r(t),n.d(t,{assets:function(){return c},contentTitle:function(){return r},default:function(){return d},frontMatter:function(){return s},metadata:function(){return o},toc:function(){return l}});var o=n(457),a=n(5893),i=n(65);let s={slug:"2018/01/captains-cookbook-part-1",title:"Captain's Cookbook: Project setup",date:new Date("2018-01-16T12:00:00.000Z"),authors:["bspeice"],tags:[]},r=void 0,c={authorsImageUrls:[void 0]},l=[{value:"Step 1: Installing <code>capnp</code>",id:"step-1-installing-capnp",level:2},{value:"Step 2: Starting a Cap'N Proto Rust project",id:"step-2-starting-a-capn-proto-rust-project",level:2},{value:"Step 3: Writing a basic schema",id:"step-3-writing-a-basic-schema",level:2},{value:"Step 4: Setting up the build process",id:"step-4-setting-up-the-build-process",level:2},{value:"Step 5: Running the build",id:"step-5-running-the-build",level:2},{value:"Step 6: Making a point",id:"step-6-making-a-point",level:2}];function p(e){let t={a:"a",code:"code",h2:"h2",li:"li",ol:"ol",p:"p",pre:"pre",ul:"ul",...(0,i.a)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(t.p,{children:"A basic introduction to getting started with Cap'N Proto."}),"\n",(0,a.jsxs)(t.p,{children:["I've been working a lot with ",(0,a.jsx)(t.a,{href:"https://capnproto.org/",children:"Cap'N Proto"})," recently with Rust, but there's a real dearth of information\non how to set up and get going quickly. In the interest of trying to get more people using this (because I think it's\nfantastic), I'm going to work through a couple of examples detailing what exactly should be done to get going."]}),"\n",(0,a.jsxs)(t.p,{children:["So, what is Cap'N Proto? It's a data serialization library. It has contemporaries with ",(0,a.jsx)(t.a,{href:"https://developers.google.com/protocol-buffers/",children:"Protobuf"}),"\nand ",(0,a.jsx)(t.a,{href:"https://google.github.io/flatbuffers/",children:"FlatBuffers"}),", but is better compared with FlatBuffers. The whole point behind it\nis to define a schema language and serialization format such that:"]}),"\n",(0,a.jsxs)(t.ol,{children:["\n",(0,a.jsx)(t.li,{children:"Applications that do not share the same base programming language can communicate"}),"\n",(0,a.jsx)(t.li,{children:"The data and schema you use can naturally evolve over time as your needs change"}),"\n"]}),"\n",(0,a.jsx)(t.p,{children:"Accompanying this are typically code generators that take the schemas you define for your application and give you back\ncode for different languages to get data to and from that schema."}),"\n",(0,a.jsx)(t.p,{children:"Now, what makes Cap'N Proto different from, say, Protobuf, is that there is no serialization/deserialization step the same way\nas is implemented with Protobuf. Instead, the idea is that the message itself can be loaded in memory and used directly there."}),"\n",(0,a.jsxs)(t.p,{children:["We're going to take a look at a series of progressively more complex projects that use Cap'N Proto in an effort to provide some\nexamples of what idiomatic usage looks like, and shorten the startup time needed to make use of this library in Rust projects.\nIf you want to follow along, feel free. If not, I've posted ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1",children:"the final result"}),"\nfor reference."]}),"\n",(0,a.jsxs)(t.h2,{id:"step-1-installing-capnp",children:["Step 1: Installing ",(0,a.jsx)(t.code,{children:"capnp"})]}),"\n",(0,a.jsxs)(t.p,{children:["The ",(0,a.jsx)(t.code,{children:"capnp"})," binary itself is needed for taking the schema files you write and turning them into a format that can be used by the\ncode generation libraries. Don't ask me what that actually means, I just know that you need to make sure this is installed."]}),"\n",(0,a.jsxs)(t.p,{children:["I'll refer you to ",(0,a.jsx)(t.a,{href:"https://capnproto.org/install.html",children:"Cap'N Proto's installation instructions"})," here. As a quick TLDR though:"]}),"\n",(0,a.jsxs)(t.ul,{children:["\n",(0,a.jsxs)(t.li,{children:["Linux users will likely have a binary shipped by their package manager - On Ubuntu, ",(0,a.jsx)(t.code,{children:"apt install capnproto"})," is enough"]}),"\n",(0,a.jsxs)(t.li,{children:["OS X users can use ",(0,a.jsx)(t.a,{href:"https://brew.sh/",children:"Homebrew"})," as an easy install path. Just ",(0,a.jsx)(t.code,{children:"brew install capnp"})]}),"\n",(0,a.jsxs)(t.li,{children:["Windows users are a bit more complicated. If you're using ",(0,a.jsx)(t.a,{href:"https://chocolatey.org/",children:"Chocolatey"}),", there's ",(0,a.jsx)(t.a,{href:"https://chocolatey.org/packages/capnproto/",children:"a package"})," available. If that doesn't work however, you need to download ",(0,a.jsx)(t.a,{href:"https://capnproto.org/capnproto-c++-win32-0.6.1.zip",children:"a release zip"})," and make sure that the ",(0,a.jsx)(t.code,{children:"capnp.exe"})," binary is in your ",(0,a.jsx)(t.code,{children:"%PATH%"})," environment variable"]}),"\n"]}),"\n",(0,a.jsx)(t.p,{children:"The way you know you're done with this step is if the following command works in your shell:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-bash",children:"capnp id\n"})}),"\n",(0,a.jsx)(t.h2,{id:"step-2-starting-a-capn-proto-rust-project",children:"Step 2: Starting a Cap'N Proto Rust project"}),"\n",(0,a.jsxs)(t.p,{children:["After the ",(0,a.jsx)(t.code,{children:"capnp"})," binary is set up, it's time to actually create our Rust project. Nothing terribly complex here, just a simple"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-bash",children:"mkdir capnp_cookbook_1\ncd capnp_cookbook_1\ncargo init --bin\n"})}),"\n",(0,a.jsxs)(t.p,{children:["We'll put the following content into ",(0,a.jsx)(t.code,{children:"Cargo.toml"}),":"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{children:'[package]\nname = "capnp_cookbook_1"\nversion = "0.1.0"\nauthors = ["Bradlee Speice <bspeice@kcg.com>"]\n\n[build-dependencies]\ncapnpc = "0.8" # 1\n\n[dependencies]\ncapnp = "0.8" # 2\n'})}),"\n",(0,a.jsx)(t.p,{children:"This sets up:"}),"\n",(0,a.jsxs)(t.ol,{children:["\n",(0,a.jsx)(t.li,{children:"The Rust code generator (CAPNProto Compiler)"}),"\n",(0,a.jsx)(t.li,{children:"The Cap'N Proto runtime library (CAPNProto runtime)"}),"\n"]}),"\n",(0,a.jsx)(t.p,{children:"We've now got everything prepared that we need for writing a Cap'N Proto project."}),"\n",(0,a.jsx)(t.h2,{id:"step-3-writing-a-basic-schema",children:"Step 3: Writing a basic schema"}),"\n",(0,a.jsx)(t.p,{children:"We're going to start with writing a pretty trivial data schema that we can extend later. This is just intended to make sure\nyou get familiar with how to start from a basic project."}),"\n",(0,a.jsx)(t.p,{children:"First, we're going to create a top-level directory for storing the schema files in:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-bash",children:"# Assuming we're starting from the `capnp_cookbook_1` directory created earlier\n\nmkdir schema\ncd schema\n"})}),"\n",(0,a.jsxs)(t.p,{children:["Now, we're going to put the following content in ",(0,a.jsx)(t.code,{children:"point.capnp"}),":"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{children:"@0xab555145c708dad2;\n\nstruct Point {\n x @0 :Int32;\n y @1 :Int32;\n}\n"})}),"\n",(0,a.jsx)(t.p,{children:"Pretty easy, we've now got structure for an object we'll be able to quickly encode in a binary format."}),"\n",(0,a.jsx)(t.h2,{id:"step-4-setting-up-the-build-process",children:"Step 4: Setting up the build process"}),"\n",(0,a.jsxs)(t.p,{children:["Now it's time to actually set up the build process to make sure that Cap'N Proto generates the Rust code we'll eventually be using.\nThis is typically done through a ",(0,a.jsx)(t.code,{children:"build.rs"})," file to invoke the schema compiler."]}),"\n",(0,a.jsxs)(t.p,{children:["In the same folder as your ",(0,a.jsx)(t.code,{children:"Cargo.toml"})," file, please put the following content in ",(0,a.jsx)(t.code,{children:"build.rs"}),":"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:'extern crate capnpc;\n\nfn main() {\n ::capnpc::CompilerCommand::new()\n .src_prefix("schema") // 1\n .file("schema/point.capnp") // 2\n .run().expect("compiling schema");\n}\n'})}),"\n",(0,a.jsxs)(t.p,{children:["This sets up the protocol compiler (",(0,a.jsx)(t.code,{children:"capnpc"})," from earlier) to compile the schema we've built so far."]}),"\n",(0,a.jsxs)(t.ol,{children:["\n",(0,a.jsxs)(t.li,{children:["Because Cap'N Proto schema files can re-use types specified in other files, the ",(0,a.jsx)(t.code,{children:"src_prefix()"})," tells the compiler\nwhere to look for those extra files at."]}),"\n",(0,a.jsxs)(t.li,{children:["We specify the schema file we're including by hand. In a much larger project, you could presumably build the ",(0,a.jsx)(t.code,{children:"CompilerCommand"}),"\ndynamically, but we won't worry too much about that one for now."]}),"\n"]}),"\n",(0,a.jsx)(t.h2,{id:"step-5-running-the-build",children:"Step 5: Running the build"}),"\n",(0,a.jsxs)(t.p,{children:["If you've done everything correctly so far, you should be able to actually build the project and see the auto-generated code.\nRun a ",(0,a.jsx)(t.code,{children:"cargo build"})," command, and if you don't see ",(0,a.jsx)(t.code,{children:"cargo"})," complaining, you're doing just fine!"]}),"\n",(0,a.jsxs)(t.p,{children:["So where exactly does the generated code go to? I think it's critically important for people to be able to see what the generated\ncode looks like, because you need to understand what you're actually programming against. The short answer is: the generated code lives\nsomewhere in the ",(0,a.jsx)(t.code,{children:"target/"})," directory."]}),"\n",(0,a.jsxs)(t.p,{children:["The long answer is that you're best off running a ",(0,a.jsx)(t.code,{children:"find"})," command to get the actual file path:"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-bash",children:"# Assuming we're running from the capnp_cookbook_1 project folder\nfind . -name point_capnp.rs\n"})}),"\n",(0,a.jsxs)(t.p,{children:["Alternately, if the ",(0,a.jsx)(t.code,{children:"find"})," command isn't available, the path will look something like:"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{children:"./target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs\n"})}),"\n",(0,a.jsx)(t.p,{children:"See if there are any paths in your target directory that look similar."}),"\n",(0,a.jsxs)(t.p,{children:["Now, the file content looks pretty nasty. I've included an example ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs",children:"here"}),"\nif you aren't following along at home. There are a couple things I'll try and point out though so you can get an idea of how\nthe schema we wrote for the \"Point\" message is tied to the generated code."]}),"\n",(0,a.jsxs)(t.p,{children:["First, the Cap'N Proto library splits things up into ",(0,a.jsx)(t.code,{children:"Builder"})," and ",(0,a.jsx)(t.code,{children:"Reader"})," structs. These are best thought of the same way\nRust separates ",(0,a.jsx)(t.code,{children:"mut"})," from non-",(0,a.jsx)(t.code,{children:"mut"})," code. ",(0,a.jsx)(t.code,{children:"Builder"}),"s are ",(0,a.jsx)(t.code,{children:"mut"})," versions of your message, and ",(0,a.jsx)(t.code,{children:"Reader"}),"s are immutable versions."]}),"\n",(0,a.jsxs)(t.p,{children:["For example, the ",(0,a.jsxs)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L90",children:[(0,a.jsx)(t.code,{children:"Builder"})," impl"]})," for ",(0,a.jsx)(t.code,{children:"point"})," defines ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L105",children:(0,a.jsx)(t.code,{children:"get_x()"})}),", ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L109",children:(0,a.jsx)(t.code,{children:"set_x()"})}),", ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L113",children:(0,a.jsx)(t.code,{children:"get_y()"})}),", and ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L117",children:(0,a.jsx)(t.code,{children:"set_y()"})})," methods.\nIn comparison, the ",(0,a.jsxs)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L38",children:[(0,a.jsx)(t.code,{children:"Reader"})," impl"]})," only defines ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L47",children:(0,a.jsx)(t.code,{children:"get_x()"})})," and ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/target/debug/build/capnp_cookbook_1-c6e2990393c32fe6/out/point_capnp.rs#L51",children:(0,a.jsx)(t.code,{children:"get_y()"})})," methods."]}),"\n",(0,a.jsxs)(t.p,{children:["So now we know that there are some ",(0,a.jsx)(t.code,{children:"get"})," and ",(0,a.jsx)(t.code,{children:"set"})," methods available for our ",(0,a.jsx)(t.code,{children:"x"})," and ",(0,a.jsx)(t.code,{children:"y"})," coordinates;\nbut what do we actually do with those?"]}),"\n",(0,a.jsx)(t.h2,{id:"step-6-making-a-point",children:"Step 6: Making a point"}),"\n",(0,a.jsxs)(t.p,{children:["So we've install Cap'N Proto, gotten a project set up, and can generate schema code now. It's time to actually start building\nCap'N Proto messages! I'm going to put the code you need here because it's small, and put some extra long comments inline. This code\nshould go in ",(0,a.jsx)(t.a,{href:"https://github.com/bspeice/capnp_cookbook_1/blob/master/src/main.rs",children:(0,a.jsx)(t.code,{children:"src/main.rs"})}),":"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"// Note that we use `capnp` here, NOT `capnpc`\nextern crate capnp;\n\n// We create a module here to define how we are to access the code\n// being included.\npub mod point_capnp {\n // The environment variable OUT_DIR is set by Cargo, and\n // is the location of all the code that was built as part\n // of the codegen step.\n // point_capnp.rs is the actual file to include\n include!(concat!(env!(\"OUT_DIR\"), \"/point_capnp.rs\"));\n}\n\nfn main() {\n\n // The process of building a Cap'N Proto message is a bit tedious.\n // We start by creating a generic Builder; it acts as the message\n // container that we'll later be filling with content of our `Point`\n let mut builder = capnp::message::Builder::new_default();\n\n // Because we need a mutable reference to the `builder` later,\n // we fence off this part of the code to allow sequential mutable\n // borrows. As I understand it, non-lexical lifetimes:\n // https://github.com/rust-lang/rust-roadmap/issues/16\n // will make this no longer necessary\n {\n // And now we can set up the actual message we're trying to create\n let mut point_msg = builder.init_root::<point_capnp::point::Builder>();\n\n // Stuff our message with some content\n point_msg.set_x(12);\n\n point_msg.set_y(14);\n }\n\n // It's now time to serialize our message to binary. Let's set up a buffer for that:\n let mut buffer = Vec::new();\n\n // And actually fill that buffer with our data\n capnp::serialize::write_message(&mut buffer, &builder).unwrap();\n\n // Finally, let's deserialize the data\n let deserialized = capnp::serialize::read_message(\n &mut buffer.as_slice(),\n capnp::message::ReaderOptions::new()\n ).unwrap();\n\n // `deserialized` is currently a generic reader; it understands\n // the content of the message we gave it (i.e. that there are two\n // int32 values) but doesn't really know what they represent (the Point).\n // This is where we map the generic data back into our schema.\n let point_reader = deserialized.get_root::<point_capnp::point::Reader>().unwrap();\n\n // We can now get our x and y values back, and make sure they match\n assert_eq!(point_reader.get_x(), 12);\n assert_eq!(point_reader.get_y(), 14);\n}\n"})}),"\n",(0,a.jsx)(t.p,{children:"And with that, we've now got a functioning project. Here's the content I'm planning to go over next as we build up\nsome practical examples of Cap'N Proto in action:"})]})}function d(e={}){let{wrapper:t}={...(0,i.a)(),...e.components};return t?(0,a.jsx)(t,{...e,children:(0,a.jsx)(p,{...e})}):p(e)}},65:function(e,t,n){n.d(t,{Z:function(){return r},a:function(){return s}});var o=n(7294);let a={},i=o.createContext(a);function s(e){let t=o.useContext(i);return o.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function r(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:s(e.components),o.createElement(i.Provider,{value:t},e.children)}},457:function(e){e.exports=JSON.parse('{"permalink":"/2018/01/captains-cookbook-part-1","source":"@site/blog/2018-01-16-captains-cookbok-part-1/index.mdx","title":"Captain\'s Cookbook: Project setup","description":"A basic introduction to getting started with Cap\'N Proto.","date":"2018-01-16T12:00:00.000Z","tags":[],"readingTime":7.555,"hasTruncateMarker":true,"authors":[{"name":"Bradlee Speice","socials":{"github":"https://github.com/bspeice"},"key":"bspeice","page":null}],"frontMatter":{"slug":"2018/01/captains-cookbook-part-1","title":"Captain\'s Cookbook: Project setup","date":"2018-01-16T12:00:00.000Z","authors":["bspeice"],"tags":[]},"unlisted":false,"lastUpdatedAt":1731201811000,"prevItem":{"title":"Captain\'s Cookbook: Practical usage","permalink":"/2018/01/captains-cookbook-part-2"},"nextItem":{"title":"PCA audio compression","permalink":"/2016/11/pca-audio-compression"}}')}}]); |