Skip to content

Commit

Permalink
Remove async_trait dependency, MSRV CI (#37)
Browse files Browse the repository at this point in the history
Requires Rust v1.75 - old enough to release now

Also fixes a few pedantic clippy things and CI improvements
  • Loading branch information
nyurik authored Apr 11, 2024
1 parent c207743 commit 64f39ec
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'

- run: |
rustc --version
Expand All @@ -34,3 +36,25 @@ jobs:
- run: cargo test
- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2

msrv:
name: Test MSRV
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'
- name: Read crate metadata
id: metadata
run: echo "rust-version=$(sed -ne 's/rust-version *= *\"\(.*\)\"/\1/p' Cargo.toml)" >> $GITHUB_OUTPUT
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ steps.metadata.outputs.rust-version }}
- run: cargo test --all-targets --all-features
- run: cargo test --features http-async
- run: cargo test --features mmap-async-tokio
- run: cargo test --features tilejson
- run: cargo test --features s3-async-native
- run: cargo test --features s3-async-rustls
- run: cargo test
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "pmtiles"
version = "0.8.0"
version = "0.9.0"
edition = "2021"
authors = ["Luke Seelenbinder <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "Implementation of the PMTiles v3 spec with multiple sync and async backends."
repository = "https://github.com/stadiamaps/pmtiles-rs"
keywords = ["pmtiles", "gis", "geo"]
rust-version = "1.68.2"
rust-version = "1.75.0"
categories = ["science::geo"]

[features]
Expand Down Expand Up @@ -35,7 +35,6 @@ __async-s3-rustls = ["rust-s3?/tokio-rustls-tls"]
# TODO: determine how we want to handle compression in async & sync environments
async-compression = { version = "0.4", features = ["gzip", "zstd", "brotli"] }
async-recursion = "1"
async-trait = "0.1"
bytes = "1"
fmmap = { version = "0.3", default-features = false, optional = true }
hilbert_2d = "1"
Expand Down
15 changes: 9 additions & 6 deletions src/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// so any file larger than 4GB, or an untrusted file with bad data may crash.
#![allow(clippy::cast_possible_truncation)]

use std::future::Future;

use async_recursion::async_recursion;
use async_trait::async_trait;
use bytes::Bytes;
#[cfg(feature = "__async")]
use tokio::io::AsyncReadExt;
Expand Down Expand Up @@ -213,23 +214,25 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
}
}

#[async_trait]
pub trait AsyncBackend {
/// Reads exactly `length` bytes starting at `offset`
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes>;
fn read_exact(
&self,
offset: usize,
length: usize,
) -> impl Future<Output = PmtResult<Bytes>> + Send;

/// Reads up to `length` bytes starting at `offset`.
async fn read(&self, offset: usize, length: usize) -> PmtResult<Bytes>;
fn read(&self, offset: usize, length: usize) -> impl Future<Output = PmtResult<Bytes>> + Send;
}

#[cfg(test)]
#[cfg(feature = "mmap-async-tokio")]
mod tests {
use super::AsyncPmTilesReader;
use crate::tests::{RASTER_FILE, VECTOR_FILE};
use crate::MmapBackend;

use super::AsyncPmTilesReader;

#[tokio::test]
async fn open_sanity_check() {
let backend = MmapBackend::try_from(RASTER_FILE).await.unwrap();
Expand Down
5 changes: 1 addition & 4 deletions src/backend_http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use bytes::Bytes;
use reqwest::header::{HeaderValue, RANGE};
use reqwest::{Client, IntoUrl, Method, Request, StatusCode, Url};
Expand Down Expand Up @@ -46,7 +45,6 @@ impl HttpBackend {
}
}

#[async_trait]
impl AsyncBackend for HttpBackend {
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
let data = self.read(offset, length).await?;
Expand Down Expand Up @@ -86,14 +84,13 @@ impl AsyncBackend for HttpBackend {
#[cfg(test)]
mod tests {
use super::*;
use crate::async_reader::AsyncPmTilesReader;

static TEST_URL: &str =
"https://protomaps.github.io/PMTiles/protomaps(vector)ODbL_firenze.pmtiles";

#[tokio::test]
async fn basic_http_test() {
let client = reqwest::Client::builder().use_rustls_tls().build().unwrap();
let client = Client::builder().use_rustls_tls().build().unwrap();
let backend = HttpBackend::try_from(client, TEST_URL).unwrap();

AsyncPmTilesReader::try_from_source(backend).await.unwrap();
Expand Down
2 changes: 0 additions & 2 deletions src/backend_mmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io;
use std::path::Path;

use async_trait::async_trait;
use bytes::{Buf, Bytes};
use fmmap::tokio::{AsyncMmapFile, AsyncMmapFileExt as _, AsyncOptions};

Expand Down Expand Up @@ -49,7 +48,6 @@ impl From<fmmap::error::Error> for PmtError {
}
}

#[async_trait]
impl AsyncBackend for MmapBackend {
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
if self.file.len() >= offset + length {
Expand Down
2 changes: 0 additions & 2 deletions src/backend_s3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use bytes::Bytes;
use s3::Bucket;

Expand Down Expand Up @@ -43,7 +42,6 @@ impl S3Backend {
}
}

#[async_trait]
impl AsyncBackend for S3Backend {
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
let data = self.read(offset, length).await?;
Expand Down
19 changes: 11 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::HashMap;
use std::future::Future;
use std::sync::{Arc, RwLock};

use async_trait::async_trait;

use crate::directory::{DirEntry, Directory};

pub enum DirCacheResult {
Expand All @@ -20,20 +19,25 @@ impl From<Option<&DirEntry>> for DirCacheResult {
}
}

/// A cache for PMTiles directories.
#[async_trait]
/// A cache for `PMTiles` directories.
pub trait DirectoryCache {
/// Get a directory from the cache, using the offset as a key.
async fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult;
fn get_dir_entry(
&self,
offset: usize,
tile_id: u64,
) -> impl Future<Output = DirCacheResult> + Send;

/// Insert a directory into the cache, using the offset as a key.
/// Note that cache must be internally mutable.
async fn insert_dir(&self, offset: usize, directory: Directory);
fn insert_dir(&self, offset: usize, directory: Directory) -> impl Future<Output = ()> + Send;
}

pub struct NoCache;

#[async_trait]
// TODO: Remove #[allow] after switching to Rust/Clippy v1.78+ in CI
// See https://github.com/rust-lang/rust-clippy/pull/12323
#[allow(clippy::no_effect_underscore_binding)]
impl DirectoryCache for NoCache {
#[inline]
async fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
Expand All @@ -50,7 +54,6 @@ pub struct HashMapCache {
pub cache: Arc<RwLock<HashMap<usize, Directory>>>,
}

#[async_trait]
impl DirectoryCache for HashMapCache {
async fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
if let Some(dir) = self.cache.read().unwrap().get(&offset) {
Expand Down

0 comments on commit 64f39ec

Please sign in to comment.