Skip to content

Commit

Permalink
Merge pull request #22 from noislabs/no-std
Browse files Browse the repository at this point in the history
The remaining no_std work
  • Loading branch information
webmaster128 committed Jun 4, 2024
2 parents 8859314 + 842463d commit b47dcc2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
build:
strategy:
matrix:
rust-version: ["1.70.0", "1.75.0"]
rust-version: ["1.70.0", "1.74.0", "1.78.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -22,10 +22,23 @@ jobs:
- run: cargo build
- run: cargo build --target wasm32-unknown-unknown

build-no_std:
strategy:
matrix:
rust-version: ["1.70.0", "1.74.0", "1.78.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "${{ matrix.rust-version }}"
targets: "thumbv6m-none-eabi"
- run: cargo check --no-default-features --target thumbv6m-none-eabi

test:
strategy:
matrix:
rust-version: ["1.70.0", "1.75.0"]
rust-version: ["1.70.0", "1.74.0", "1.78.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -34,10 +47,22 @@ jobs:
toolchain: "${{ matrix.rust-version }}"
- run: cargo test

test-no_std:
strategy:
matrix:
rust-version: ["1.70.0", "1.74.0", "1.78.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "${{ matrix.rust-version }}"
- run: cargo test --no-default-features

clippy:
strategy:
matrix:
rust-version: ["1.70.0", "1.75.0"]
rust-version: ["1.70.0", "1.74.0", "1.78.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ let deserialized = Bufany::deserialize(&data).unwrap(); // data from above
let id = deserialized.uint64(1).unwrap(); // 4
let title = deserialized.string(2).unwrap(); // "hello"
```

## `no_std` support

Since version 0.5.0 there is a default `std` feature. If you remove that, the library is built with `no_std` support.
As the anybuf maintainers do not require `no_std` support this is provided at a best effort basis and might be broken.

```toml
[dependencies]
anybuf = { version = "0.5.0", default-features = false }
```
5 changes: 5 additions & 0 deletions src/anybuf.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use crate::varint::{to_zigzag32, to_zigzag64, unsigned_varint_encode};

/// The protobuf wire types
Expand Down Expand Up @@ -538,6 +540,9 @@ mod tests {
use super::*;
use hex_literal::hex;

use alloc::string::{String, ToString};
use alloc::vec;

#[test]
fn new_returns_empty_data() {
let data = Anybuf::new();
Expand Down
8 changes: 6 additions & 2 deletions src/bufany.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::HashMap;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use crate::{
slice_reader::SliceReader,
Expand Down Expand Up @@ -34,7 +36,7 @@ use crate::{
#[derive(Debug)]
pub struct Bufany<'a> {
// A map from field number to decoded value.
fields: HashMap<u32, Vec<Value<'a>>>,
fields: BTreeMap<u32, Vec<Value<'a>>>,
// A vector that is always empty and has the lifetime we need.
empty_vec: Vec<Value<'a>>,
}
Expand Down Expand Up @@ -820,6 +822,8 @@ mod tests {
use crate::Anybuf;

use super::*;
use alloc::string::ToString;
use alloc::vec;

#[test]
fn deserialize_works() {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
//! - Packed encoding for repeated fields
//! - Maps support (but you can use the equivalent [encoding via repeated messages](https://protobuf.dev/programming-guides/encoding/#maps))

#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

mod anybuf;
mod bufany;
mod slice_reader;
Expand Down
2 changes: 2 additions & 0 deletions src/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl<'x> SliceReader<'x> {
mod tests {
use super::*;

use alloc::vec;

#[test]
fn read_array_works() {
let original = vec![5u8, 7, 234, 2, 45, 0, 12, 32, 192];
Expand Down
4 changes: 4 additions & 0 deletions src/varint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use crate::slice_reader::SliceReader;

#[allow(dead_code)]
Expand Down Expand Up @@ -69,6 +71,8 @@ pub fn read_unsigned_varint(data: &mut SliceReader) -> Option<u64> {
mod tests {
use super::*;

use alloc::vec;

#[test]
fn to_zigzag32_works() {
// values from https://protobuf.dev/programming-guides/encoding/
Expand Down

0 comments on commit b47dcc2

Please sign in to comment.