Commit 8c9fade2 authored by Ticki's avatar Ticki
Browse files

'Example' and misc changes

parent 64000d14
......@@ -23,6 +23,8 @@
- [Exploring Redox](./getting_started/exploring_redox.md)
- [Questions and feedback](./getting_started/asking_questions_giving_feedback.md)
-------------------------------------------------------------------------------
- [The design](./design/design.md)
- [URLs, schemes and resources](./design/urls_schemes_resources.md)
- [URLs](./design/url/urls.md)
......@@ -37,7 +39,7 @@
- [File-like](./design/resource/file_like..md)
- [Stitching it all together](./design/url_scheme_resource/stiching_it_all_together.md)
- ["Everything is a URL"](./design/url_scheme_resource/everything_is_a_url.md)
- [An example]()
- [An example](./design/url_scheme_resource/example.md)
- [The kernel]()
- [Microkernels]()
- [Advantages of microkernels]()
......@@ -97,6 +99,8 @@
- [Remove dependencies]()
- [Cross-compiling]()
-------------------------------------------------------------------------------
- [Contributing]()
- [Communication]()
- [Chat]()
......@@ -134,6 +138,8 @@
- [Kernel]()
- [Environment]()
-------------------------------------------------------------------------------
- [Fun]()
- [Running Redox on real hardware]()
- [Setting up a Redox desktop]()
......
......@@ -5,6 +5,8 @@
By unifying the API in this way, you get an extremely consistent, clean, and flexible interface.
We can't really claim credits of this concept (except for the implementation and exact design). The idea is not a new one: The concept is very similar to _9P_ from _Plan 9_ by Bell Labs.
How it differs from "everything is a file"
------------------------------------------
......
An example.
===========
Enough theory! Time for an example.
We will implement a scheme which holds a vector, and push elements when you write, and pop them when you read. Let's call it `vec:`.
Let's get goin':
The code
--------
So first of all we need to import the things, we need:
```rust
use system::scheme::Scheme;
use system::error::{Error, Result, ENOENT, EBADF, EINVAL};
use std::cmp::min;
```
We start by defining our scheme struct, which will implement the `Scheme` trait and hold the state of the scheme.
```rust
struct VecScheme {
vec: Vec<u8>,
}
impl VecScheme {
fn new() -> VecScheme {
VecScheme {
vec: Vec::new(),
}
}
}
```
First of all we implement, `open()`. Let it accept a reference, which will be the initial content of the vector.
Note that we do ignore `flags` and `mode`.
```rust
impl Scheme for VecScheme {
fn open(&mut self, path: &str, _flags: usize, _mode: usize) -> Result<usize> {
self.vec = path.as_bytes().to_owned();
Result::Ok(path.len())
}
```
So, now we implement read
```rust
fn read(&mut self, _: usize, buf: &mut [u8]) -> Result<usize> {
let res = min(buf.len(), self.vec.len());
for b in buf {
*b = if let Some(x) = self.vec.pop() {
x
} else {
break;
}
}
Result::Ok(res)
}
```
Now, we will add `write`, which will simply push to the vector:
```rust
fn write(&mut self, _: usize, buf: &[u8]) -> Result<usize> {
for &i in buf {
self.vec.push(i);
}
Result::Ok(buf.len())
}
}
```
Note that leaving out the missing methods results in errors, when calling them.
Last, we need the `main` function:
```rust
fn main() {
use system::scheme::Packet;
let mut scheme = VecScheme::new();
// Create the handler
let mut socket = File::create(":vec").unwrap();
loop {
let mut packet = Packet::default();
while socket.read(&mut packet).unwrap() == size_of::<Packet>() {
scheme.handle(&mut packet);
socket.write(&packet).unwrap();
}
}
}
```
Now, go ahead and test it!
Exercise for the reader
------------------------
Write a scheme with can run Brainfuck code.
......@@ -19,11 +19,15 @@ The "core team" (people who are regularly contributing to Redox) is currently:
(alphabetically sorted)
- alicemaz
- eghiorzi
- hauleth
- henrikhodne
- jackpot51
- k0pernicus
- mgattozzi
- mmstick
- nounoursheureux
- roxxik
- skylerberg
- stratact
......
Supports Markdown
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