Skip to content

Commit

Permalink
Enable build for wasm32-wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
adamnovak committed Dec 5, 2023
1 parent c2f8637 commit 8c4736f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/int_vector.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! A bit-packed integer vector storing fixed-width integers.

use crate::ops::{Vector, Resize, Pack, Access, AccessIter, Push, Pop};
use crate::raw_vector::{RawVector, RawVectorMapper, RawVectorWriter, AccessRaw, PushRaw, PopRaw};
use crate::serialize::{MemoryMap, MemoryMapped, Serialize};
use crate::raw_vector::{RawVector, RawVectorWriter, AccessRaw, PushRaw, PopRaw};
#[cfg(not(target_family = "wasm"))]
use crate::raw_vector::RawVectorMapper;
use crate::serialize::Serialize;
#[cfg(not(target_family = "wasm"))]
use crate::serialize::{MemoryMap, MemoryMapped};
use crate::bits;

use std::io::{Error, ErrorKind};
Expand Down Expand Up @@ -580,13 +584,15 @@ impl Drop for IntVectorWriter {
/// drop(mapper); drop(map);
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct IntVectorMapper<'a> {
len: usize,
width: usize,
data: RawVectorMapper<'a>,
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Vector for IntVectorMapper<'a> {
type Item = u64;

Expand All @@ -606,6 +612,7 @@ impl<'a> Vector for IntVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Access<'a> for IntVectorMapper<'a> {
type Iter = AccessIter<'a, Self>;

Expand All @@ -620,6 +627,7 @@ impl<'a> Access<'a> for IntVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MemoryMapped<'a> for IntVectorMapper<'a> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset + 1 >= map.len() {
Expand All @@ -643,6 +651,7 @@ impl<'a> MemoryMapped<'a> for IntVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AsRef<RawVectorMapper<'a>> for IntVectorMapper<'a> {
#[inline]
fn as_ref(&self) -> &RawVectorMapper<'a> {
Expand Down
9 changes: 8 additions & 1 deletion src/raw_vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! The basic vector implementing the low-level functionality used by other vectors in the crate.

use crate::serialize::{MappedSlice, MemoryMap, MemoryMapped, Serialize};
use crate::serialize::Serialize;
#[cfg(not(target_family = "wasm"))]
use crate::serialize::{MappedSlice, MemoryMap, MemoryMapped};
use crate::bits;

use std::fs::{File, OpenOptions};
Expand Down Expand Up @@ -906,12 +908,14 @@ impl Drop for RawVectorWriter {
/// drop(mapper); drop(map);
/// fs::remove_file(&filename);
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct RawVectorMapper<'a> {
len: usize,
data: MappedSlice<'a, u64>,
}

#[cfg(not(target_family = "wasm"))]
impl<'a> RawVectorMapper<'a> {
/// Returns the length of the vector in bits.
#[inline]
Expand All @@ -935,6 +939,7 @@ impl<'a> RawVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AccessRaw for RawVectorMapper<'a> {
#[inline]
fn bit(&self, bit_offset: usize) -> bool {
Expand Down Expand Up @@ -973,6 +978,7 @@ impl<'a> AccessRaw for RawVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MemoryMapped<'a> for RawVectorMapper<'a> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset >= map.len() {
Expand All @@ -995,6 +1001,7 @@ impl<'a> MemoryMapped<'a> for RawVectorMapper<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AsRef<MappedSlice<'a, u64>> for RawVectorMapper<'a> {
#[inline]
fn as_ref(&self) -> &MappedSlice<'a, u64> {
Expand Down
38 changes: 35 additions & 3 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@
use crate::bits;

use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::fs::OpenOptions;
#[cfg(not(target_family = "wasm"))]
use std::fs::File;
use std::io::{Error, ErrorKind, Read, Write};
#[cfg(not(target_family = "wasm"))]
use std::ops::{Deref, Index};
use std::os::unix::io::AsRawFd;
#[cfg(not(target_family = "wasm"))]
use std::os::fd::AsRawFd;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{env, fs, io, marker, mem, process, ptr, slice, str};
use std::{env, fs, io, mem, process, slice, str};
#[cfg(not(target_family = "wasm"))]
use std::{marker, ptr};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -348,6 +354,7 @@ impl<V: Serialize> Serialize for Option<V> {
//-----------------------------------------------------------------------------

/// Modes of memory mapping a file.
#[cfg(not(target_family = "wasm"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MappingMode {
/// The file is read-only.
Expand Down Expand Up @@ -386,6 +393,7 @@ pub enum MappingMode {
/// drop(map);
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(Debug)]
pub struct MemoryMap {
_file: File, // The compiler might otherwise get overzealous and complain that we don't touch the file.
Expand All @@ -396,6 +404,7 @@ pub struct MemoryMap {
}

// TODO: implement madvise()?
#[cfg(not(target_family = "wasm"))]
impl MemoryMap {
/// Returns a memory map for the specified file in the given mode.
///
Expand Down Expand Up @@ -479,12 +488,14 @@ impl MemoryMap {
}
}

#[cfg(not(target_family = "wasm"))]
impl AsRef<[u64]> for MemoryMap {
fn as_ref(&self) -> &[u64] {
unsafe { slice::from_raw_parts(self.ptr, self.len) }
}
}

#[cfg(not(target_family = "wasm"))]
impl Drop for MemoryMap {
fn drop(&mut self) {
unsafe {
Expand Down Expand Up @@ -556,6 +567,7 @@ impl Drop for MemoryMap {
///
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
pub trait MemoryMapped<'a>: Sized {
/// Returns an immutable memory-mapped structure corresponding to an interval in the file.
///
Expand Down Expand Up @@ -604,12 +616,14 @@ pub trait MemoryMapped<'a>: Sized {
///
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct MappedSlice<'a, T: Serializable> {
data: &'a [T],
offset: usize,
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: Serializable> MappedSlice<'a, T> {
/// Returns the length of the slice.
pub fn len(&self) -> usize {
Expand All @@ -622,12 +636,14 @@ impl<'a, T: Serializable> MappedSlice<'a, T> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: Serializable> AsRef<[T]> for MappedSlice<'a, T> {
fn as_ref(&self) -> &[T] {
self.data
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: Serializable> Deref for MappedSlice<'a, T> {
type Target = [T];

Expand All @@ -636,6 +652,7 @@ impl<'a, T: Serializable> Deref for MappedSlice<'a, T> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: Serializable> Index<usize> for MappedSlice<'a, T> {
type Output = T;

Expand All @@ -644,6 +661,7 @@ impl<'a, T: Serializable> Index<usize> for MappedSlice<'a, T> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: Serializable> MemoryMapped<'a> for MappedSlice<'a, T> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset >= map.len() {
Expand Down Expand Up @@ -697,12 +715,14 @@ impl<'a, T: Serializable> MemoryMapped<'a> for MappedSlice<'a, T> {
///
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct MappedBytes<'a> {
data: &'a [u8],
offset: usize,
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MappedBytes<'a> {
/// Returns the length of the slice.
pub fn len(&self) -> usize {
Expand All @@ -715,12 +735,14 @@ impl<'a> MappedBytes<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AsRef<[u8]> for MappedBytes<'a> {
fn as_ref(&self) -> &[u8] {
self.data
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Deref for MappedBytes<'a> {
type Target = [u8];

Expand All @@ -729,6 +751,7 @@ impl<'a> Deref for MappedBytes<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Index<usize> for MappedBytes<'a> {
type Output = u8;

Expand All @@ -737,6 +760,7 @@ impl<'a> Index<usize> for MappedBytes<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MemoryMapped<'a> for MappedBytes<'a> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset >= map.len() {
Expand Down Expand Up @@ -788,12 +812,14 @@ impl<'a> MemoryMapped<'a> for MappedBytes<'a> {
///
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct MappedStr<'a> {
data: &'a str,
offset: usize,
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MappedStr<'a> {
/// Returns the length of the slice in bytes.
pub fn len(&self) -> usize {
Expand All @@ -806,19 +832,22 @@ impl<'a> MappedStr<'a> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AsRef<str> for MappedStr<'a> {
fn as_ref(&self) -> &str {
self.data
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Deref for MappedStr<'a> {
type Target = str;

fn deref(&self) -> &Self::Target {
self.data
}
}
#[cfg(not(target_family = "wasm"))]
impl<'a> MemoryMapped<'a> for MappedStr<'a> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset >= map.len() {
Expand Down Expand Up @@ -870,6 +899,7 @@ impl<'a> MemoryMapped<'a> for MappedStr<'a> {
///
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct MappedOption<'a, T: MemoryMapped<'a>> {
data: Option<T>,
Expand All @@ -878,6 +908,7 @@ pub struct MappedOption<'a, T: MemoryMapped<'a>> {
_marker: marker::PhantomData<&'a ()>,
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: MemoryMapped<'a>> MappedOption<'a, T> {
/// Returns `true` if the option is a [`Some`] value.
pub fn is_some(&self) -> bool {
Expand Down Expand Up @@ -910,6 +941,7 @@ impl<'a, T: MemoryMapped<'a>> MappedOption<'a, T> {
}
}

#[cfg(not(target_family = "wasm"))]
impl<'a, T: MemoryMapped<'a>> MemoryMapped<'a> for MappedOption<'a, T> {
fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
if offset >= map.len() {
Expand Down

0 comments on commit 8c4736f

Please sign in to comment.