Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 022ca7c

Browse files
committedMay 24, 2020
Add support for no-std usages of capnp crate.
1 parent 1f99e45 commit 022ca7c

33 files changed

+386
-192
lines changed
 

‎.travis.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ sudo: false
33
language: rust
44
matrix:
55
include:
6-
- rust: stable
6+
#- rust: stable
77
- rust: beta
88
- rust: nightly
9-
- rust: 1.40.0
9+
#- rust: 1.44.0
1010
env:
1111
global:
1212
- export PATH="$PATH:$HOME/bin"
@@ -26,6 +26,9 @@ install:
2626
- cd ../
2727

2828
script:
29+
- cd capnp
30+
- cargo test --no-default-features
31+
- cd ../
2932
- cargo build --all
3033
- cargo test --all
3134
- ./target/debug/addressbook write | ./target/debug/addressbook read

‎capnp/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ quickcheck = { version = "0.9", optional = true }
2525
quickcheck = "0.9"
2626

2727
[features]
28+
default = ["std"]
29+
2830
rpc_try = []
2931

3032
# If enabled, relaxes alignment requirements on segments.
3133
# This has a performance cost on some targets (e.g. ARMv6).
3234
unaligned = []
3335

36+
std = []
37+

‎capnp/src/any_pointer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
//! Dynamically typed value.
2323
24+
use alloc::boxed::Box;
25+
use alloc::vec::Vec;
26+
2427
use crate::capability::FromClientHook;
2528
use crate::private::capability::{ClientHook, PipelineHook, PipelineOp};
2629
use crate::private::layout::{PointerReader, PointerBuilder};

‎capnp/src/any_pointer_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl <'a> crate::traits::SetPointerBuilder<Builder<'a>> for Reader<'a> {
124124
}
125125
}
126126

127-
impl <'a> ::std::iter::IntoIterator for Reader<'a> {
127+
impl <'a> core::iter::IntoIterator for Reader<'a> {
128128
type Item = Result<crate::any_pointer::Reader<'a>>;
129129
type IntoIter = ListIter<Reader<'a>, Self::Item>;
130130

‎capnp/src/capability.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@
2323
//!
2424
//! Roughly corresponds to capability.h in the C++ implementation.
2525
26+
use alloc::boxed::Box;
27+
use core::future::{Future};
28+
use core::pin::{Pin};
29+
use core::marker::{PhantomData, Unpin};
30+
use core::task::Poll;
31+
#[cfg(feature = "rpc_try")]
32+
use core::ops::Try;
33+
2634
use crate::{any_pointer, Error, MessageSize};
2735
use crate::traits::{Pipelined, Owned};
2836
use crate::private::capability::{ClientHook, ParamsHook, RequestHook, ResponseHook, ResultsHook};
2937

30-
use std::future::{Future};
31-
use std::pin::{Pin};
32-
use std::marker::Unpin;
33-
use std::task::Poll;
34-
#[cfg(feature = "rpc_try")]
35-
use std::ops::Try;
36-
37-
use std::marker::PhantomData;
38-
3938
/// A computation that might eventually resolve to a value of type `T` or to an error
4039
/// of type `E`. Dropping the promise cancels the computation.
4140
#[must_use = "futures do nothing unless polled"]
@@ -45,7 +44,7 @@ pub struct Promise<T, E> {
4544

4645
enum PromiseInner<T, E> {
4746
Immediate(Result<T,E>),
48-
Deferred(Pin<Box<dyn Future<Output=std::result::Result<T,E>> + 'static>>),
47+
Deferred(Pin<Box<dyn Future<Output=core::result::Result<T,E>> + 'static>>),
4948
Empty,
5049
}
5150

@@ -62,21 +61,21 @@ impl <T, E> Promise<T, E> {
6261
}
6362

6463
pub fn from_future<F>(f: F) -> Promise<T, E>
65-
where F: Future<Output=std::result::Result<T,E>> + 'static
64+
where F: Future<Output=core::result::Result<T,E>> + 'static
6665
{
6766
Promise { inner: PromiseInner::Deferred(Box::pin(f)) }
6867
}
6968
}
7069

7170
impl <T, E> Future for Promise<T, E>
7271
{
73-
type Output = std::result::Result<T,E>;
72+
type Output = core::result::Result<T,E>;
7473

75-
fn poll(self: Pin<&mut Self>, cx: &mut ::std::task::Context) -> Poll<Self::Output> {
74+
fn poll(self: Pin<&mut Self>, cx: &mut ::core::task::Context) -> Poll<Self::Output> {
7675
match self.get_mut().inner {
7776
PromiseInner::Empty => panic!("Promise polled after done."),
7877
ref mut imm @ PromiseInner::Immediate(_) => {
79-
match std::mem::replace(imm, PromiseInner::Empty) {
78+
match core::mem::replace(imm, PromiseInner::Empty) {
8079
PromiseInner::Immediate(r) => Poll::Ready(r),
8180
_ => unreachable!(),
8281
}
@@ -254,7 +253,7 @@ pub trait Server {
254253
/// Trait to track the relationship between generated Server traits and Client structs.
255254
pub trait FromServer<S> : FromClientHook {
256255
// Implemented by the generated ServerDispatch struct.
257-
type Dispatch: Server + 'static + std::ops::DerefMut<Target=S>;
256+
type Dispatch: Server + 'static + core::ops::DerefMut<Target=S>;
258257

259258
fn from_server(s: S) -> Self::Dispatch;
260259
}

‎capnp/src/capability_list.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
//! List of capabilities.
2222
23-
use std::marker::PhantomData;
23+
use alloc::boxed::Box;
24+
use core::marker::PhantomData;
2425

2526
use crate::capability::{FromClientHook};
2627
use crate::private::capability::ClientHook;
@@ -152,7 +153,7 @@ impl <'a, T> crate::traits::SetPointerBuilder<Builder<'a, T>> for Reader<'a, T>
152153
}
153154
}
154155

155-
impl <'a, T> ::std::iter::IntoIterator for Reader<'a, T>
156+
impl <'a, T> ::core::iter::IntoIterator for Reader<'a, T>
156157
where T: FromClientHook
157158
{
158159
type Item = Result<T>;

‎capnp/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! `constant::Reader` does not do bounds-checking, so it is unsafe to
2424
//! manually construct one of these.
2525
26-
use std::marker::PhantomData;
26+
use core::marker::PhantomData;
2727

2828
use crate::any_pointer;
2929
use crate::private::layout::PointerReader;

‎capnp/src/data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> crate::traits::Owned<'a> for Owned {
3535
pub type Reader<'a> = &'a [u8];
3636

3737
pub fn new_reader<'a>(p : *const u8, len : u32) -> Reader<'a> {
38-
unsafe { ::std::slice::from_raw_parts(p, len as usize) }
38+
unsafe { ::core::slice::from_raw_parts(p, len as usize) }
3939
}
4040

4141
impl <'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
@@ -47,7 +47,7 @@ impl <'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
4747
pub type Builder<'a> = &'a mut [u8];
4848

4949
pub fn new_builder<'a>(p : *mut u8, len : u32) -> Builder<'a> {
50-
unsafe { ::std::slice::from_raw_parts_mut(p, len as usize) }
50+
unsafe { ::core::slice::from_raw_parts_mut(p, len as usize) }
5151
}
5252

5353
impl <'a> crate::traits::FromPointerBuilder<'a> for Builder<'a> {

‎capnp/src/data_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl <'a> crate::traits::SetPointerBuilder<Builder<'a>> for Reader<'a> {
133133
}
134134
}
135135

136-
impl <'a> ::std::iter::IntoIterator for Reader<'a> {
136+
impl <'a> ::core::iter::IntoIterator for Reader<'a> {
137137
type Item = Result<crate::data::Reader<'a>>;
138138
type IntoIter = ListIter<Reader<'a>, Self::Item>;
139139

‎capnp/src/enum_list.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::private::layout::{ListReader, ListBuilder, PointerReader, PointerBuil
2727
TwoBytes, PrimitiveElement};
2828
use crate::{NotInSchema, Result};
2929

30-
use std::marker::PhantomData;
30+
use core::marker::PhantomData;
3131

3232
#[derive(Clone, Copy)]
3333
pub struct Owned<T> {
@@ -52,7 +52,7 @@ impl <'a, T: FromU16> Reader<'a, T> {
5252

5353
pub fn len(&self) -> u32 { self.reader.len() }
5454

55-
pub fn iter(self) -> ListIter<Reader<'a, T>, ::std::result::Result<T, NotInSchema>>{
55+
pub fn iter(self) -> ListIter<Reader<'a, T>, ::core::result::Result<T, NotInSchema>>{
5656
let l = self.len();
5757
ListIter::new(self, l)
5858
}
@@ -65,14 +65,14 @@ impl <'a, T : FromU16> FromPointerReader<'a> for Reader<'a, T> {
6565
}
6666
}
6767

68-
impl <'a, T: FromU16> IndexMove<u32, ::std::result::Result<T, NotInSchema>> for Reader<'a, T>{
69-
fn index_move(&self, index: u32) -> ::std::result::Result<T, NotInSchema> {
68+
impl <'a, T: FromU16> IndexMove<u32, ::core::result::Result<T, NotInSchema>> for Reader<'a, T>{
69+
fn index_move(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
7070
self.get(index)
7171
}
7272
}
7373

7474
impl <'a, T : FromU16> Reader<'a, T> {
75-
pub fn get(&self, index: u32) -> ::std::result::Result<T, NotInSchema> {
75+
pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
7676
assert!(index < self.len());
7777
let result: u16 = PrimitiveElement::get(&self.reader, index);
7878
FromU16::from_u16(result)
@@ -119,7 +119,7 @@ impl <'a, T : FromU16> FromPointerBuilder<'a> for Builder<'a, T> {
119119
}
120120

121121
impl <'a, T : ToU16 + FromU16> Builder<'a, T> {
122-
pub fn get(&self, index: u32) -> ::std::result::Result<T, NotInSchema> {
122+
pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
123123
assert!(index < self.len());
124124
let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
125125
FromU16::from_u16(result)
@@ -138,8 +138,8 @@ impl <'a, T> crate::traits::SetPointerBuilder<Builder<'a, T>> for Reader<'a, T>
138138
}
139139
}
140140

141-
impl <'a, T: FromU16> ::std::iter::IntoIterator for Reader<'a, T> {
142-
type Item = ::std::result::Result<T, NotInSchema>;
141+
impl <'a, T: FromU16> ::core::iter::IntoIterator for Reader<'a, T> {
142+
type Item = ::core::result::Result<T, NotInSchema>;
143143
type IntoIter = ListIter<Reader<'a, T>, Self::Item>;
144144

145145
fn into_iter(self) -> Self::IntoIter {

‎capnp/src/io.rs

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//! Custom I/O traits that roughly mirror `std::io::{Read, BufRead, Write}`.
2+
//! This extra layer of indirection enables support of no-std environments.
3+
4+
use crate::Result;
5+
6+
pub trait Read {
7+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
8+
}
9+
10+
pub trait BufRead : Read {
11+
fn fill_buf(&mut self) -> Result<&[u8]>;
12+
fn consume(&mut self, amt: usize);
13+
}
14+
15+
pub trait Write {
16+
fn write_all(&mut self, buf: &[u8]) -> Result<()>;
17+
}
18+
19+
#[cfg(feature="std")]
20+
mod std_impls {
21+
use crate::{Result};
22+
use crate::io::{Read, BufRead, Write};
23+
24+
impl <R> Read for R where R: std::io::Read {
25+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
26+
std::io::Read::read_exact(self, buf)?;
27+
Ok(())
28+
}
29+
}
30+
31+
impl <R> BufRead for R where R: std::io::BufRead {
32+
fn fill_buf(&mut self) -> Result<&[u8]> {
33+
Ok(std::io::BufRead::fill_buf(self)?)
34+
}
35+
fn consume(&mut self, amt: usize) {
36+
std::io::BufRead::consume(self, amt)
37+
}
38+
}
39+
40+
impl <W> Write for W where W: std::io::Write {
41+
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
42+
std::io::Write::write_all(self, buf)?;
43+
Ok(())
44+
}
45+
}
46+
}
47+
48+
#[cfg(not(feature="std"))]
49+
mod no_std_impls {
50+
use alloc::string::ToString;
51+
use crate::{Error, Result};
52+
use crate::io::{Read, BufRead, Write};
53+
54+
impl <'a> Write for &'a mut [u8] {
55+
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
56+
if buf.len() > self.len() {
57+
return Err(Error::failed("buffer is not large enough".to_string()));
58+
}
59+
let amt = buf.len();
60+
let (a, b) = core::mem::replace(self, &mut []).split_at_mut(amt);
61+
a.copy_from_slice(buf);
62+
*self = b;
63+
Ok(())
64+
}
65+
}
66+
67+
impl Write for alloc::vec::Vec<u8> {
68+
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
69+
self.extend_from_slice(buf);
70+
Ok(())
71+
}
72+
}
73+
74+
impl <W: ?Sized> Write for &mut W where W: Write {
75+
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
76+
(**self).write_all(buf)
77+
}
78+
}
79+
80+
impl <'a> Read for &'a [u8] {
81+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
82+
if buf.len() > self.len() {
83+
return Err(Error::failed("buffer is not large enough".to_string()));
84+
}
85+
let (a, b) = self.split_at(buf.len());
86+
buf.copy_from_slice(a);
87+
*self = b;
88+
Ok(())
89+
}
90+
}
91+
92+
impl <R: ?Sized> Read for &mut R where R: Read {
93+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
94+
(**self).read_exact(buf)
95+
}
96+
}
97+
98+
impl <'a> BufRead for &'a [u8] {
99+
fn fill_buf(&mut self) -> Result<&[u8]> {
100+
Ok(*self)
101+
}
102+
fn consume(&mut self, amt: usize) {
103+
*self = &self[amt..]
104+
}
105+
}
106+
107+
impl <R: ?Sized> BufRead for &mut R where R: BufRead {
108+
fn fill_buf(&mut self) -> Result<&[u8]> {
109+
(**self).fill_buf()
110+
}
111+
fn consume(&mut self, amt: usize) {
112+
(**self).consume(amt)
113+
}
114+
}
115+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.