Skip to content

Commit

Permalink
Initial steps toward making std an optional feature. (#81)
Browse files Browse the repository at this point in the history
* feat: change some imports from std to core

As part of #20, we'd like to make `std` into an optional
dependency for the `gitoid` crate, so it can be used in `no_std`
environments. This requires both transitioning usage of dependencies
to `no_std`, and moving our own imports away from `std`.

As a first step, this commit transitions just the imports which
are direct re-exports of things from `core`, to use the original
`core` imports instead. We're not ready for `no_std` yet, but this
does move us closer.

* feat: start move to no_std deps

Per #20, we want to make `gitoid` `no_std`-compatible. Three of
our deps: `hex`, `sha1`, and `sha2` are `no_std`-compatible. Of
those, only `sha2` appears to be able to transition to turn off
`std` immediately without impact to the APIs we use. For `hex`
and `sha1`, we'll need to update our usage of the APIs to avoid
the things that are turned off when the import becomes `no_std`.

This commit doesn't undertake the work of actually transitioning
the usage, but _does_ update the `Cargo.toml` file to:

- Transition `sha2` to `no_std` immediately.
- Update our import of `hex` and `sha1` to turn off default
  features and then purposefully activate the `std` feature. This
  makes the status of using `std` more clear, and paves for way
  for removing the manual activation in the future when we've
  updated our usage of those deps.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker authored Dec 28, 2023
1 parent 8d88434 commit 8d790c7
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
6 changes: 3 additions & 3 deletions gitoid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "0.1.5"
crate-type = ["rlib", "cdylib"]

[dependencies]
hex = "0.4.3"
sha1 = "0.10.6"
sha2 = "0.10.8"
hex = { version = "0.4.3", default-features = false, features = ["std"] }
sha1 = { version = "0.10.6", default-features = false, features = ["std"] }
sha2 = { version = "0.10.8", default-features = false }
url = "2.4.1"
11 changes: 6 additions & 5 deletions gitoid/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Error arising from `GitOid` construction or use.
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result as FmtResult;
use core::result::Result as StdResult;
use hex::FromHexError as HexError;
use std::error::Error as StdError;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io::Error as IoError;
use url::ParseError as UrlError;
use url::Url;

/// A `Result` with `gitoid::Error` as the error type.
pub(crate) type Result<T> = std::result::Result<T, Error>;
pub(crate) type Result<T> = StdResult<T, Error>;

/// An error arising during `GitOid` construction or use.
#[derive(Debug)]
Expand Down Expand Up @@ -39,7 +40,7 @@ pub enum Error {
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Error::BadLength { expected, actual } => {
write!(f, "expected length {}, actual length {}", expected, actual)
Expand Down
14 changes: 7 additions & 7 deletions gitoid/src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
//! errors to users of the `gitoid` FFI.
use crate::error::Error as GitOidError;
use std::any::Any;
use std::cell::RefCell;
use core::any::Any;
use core::cell::RefCell;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result as FmtResult;
use core::panic::UnwindSafe;
use core::str::Utf8Error;
use std::error::Error as StdError;
use std::ffi::NulError;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::panic::catch_unwind;
use std::panic::UnwindSafe;
use std::str::Utf8Error;
use url::ParseError as UrlError;

thread_local! {
Expand Down
17 changes: 9 additions & 8 deletions gitoid/src/ffi/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ use crate::ffi::util::write_to_c_buf;
use crate::GitOid;
use crate::HashAlgorithm;
use crate::ObjectType;
use std::ffi::c_char;
use std::ffi::c_int;
use std::ffi::CStr;
use core::ffi::c_char;
use core::ffi::c_int;
use core::ffi::CStr;
use core::ptr::null;
use core::ptr::null_mut;
use core::slice::from_raw_parts;
use core::slice::from_raw_parts_mut;
use std::ffi::CString;
use std::fs::File;
use std::io::BufReader;
Expand All @@ -23,9 +27,6 @@ use std::os::unix::prelude::RawFd;
use std::os::windows::io::FromRawHandle;
#[cfg(target_family = "windows")]
use std::os::windows::prelude::RawHandle;
use std::ptr::null;
use std::ptr::null_mut;
use std::slice;
use url::Url;

/// Get the last-written error message written to a buffer.
Expand All @@ -44,7 +45,7 @@ pub extern "C" fn gitoid_get_error_message(buffer: *mut c_char, length: c_int) -
}

// Convert the buffer raw pointer into a byte slice.
let buffer = unsafe { slice::from_raw_parts_mut(buffer as *mut u8, length as usize) };
let buffer = unsafe { from_raw_parts_mut(buffer as *mut u8, length as usize) };

// Get the last error, possibly empty if there isn't one.
let last_err = get_error_msg().unwrap_or_default();
Expand Down Expand Up @@ -87,7 +88,7 @@ pub extern "C" fn gitoid_new_from_bytes(
) -> GitOid {
let output = catch_panic(|| {
check_null(content, Error::ContentPtrIsNull)?;
let content = unsafe { slice::from_raw_parts(content, content_len) };
let content = unsafe { from_raw_parts(content, content_len) };
Ok(GitOid::new_from_bytes(hash_algorithm, object_type, content))
});

Expand Down
2 changes: 1 addition & 1 deletion gitoid/src/ffi/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::ffi::error::Error;
use crate::ffi::status::Status;
use std::ffi::c_int;
use core::ffi::c_int;
use std::ffi::CString;
use std::io::Write as _;

Expand Down
9 changes: 5 additions & 4 deletions gitoid/src/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ use crate::HashRef;
use crate::ObjectType;
use crate::Result;
use crate::NUM_HASH_BYTES;
use core::cmp::min;
use core::fmt;
use core::fmt::Display;
use core::fmt::Formatter;
use core::hash::Hash;
use core::ops::Not as _;
use core::str::FromStr;
use sha2::digest::DynDigest;
use std::hash::Hash;
use std::io::BufReader;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::ops::Not as _;
use std::str::FromStr;
use url::Url;

/// A struct that computes [gitoids][g] based on the selected algorithm
Expand Down Expand Up @@ -259,7 +260,7 @@ where
let hash = digester.finalize();
let mut ret = [0u8; NUM_HASH_BYTES];

let len = std::cmp::min(NUM_HASH_BYTES, hash.len());
let len = min(NUM_HASH_BYTES, hash.len());
ret[..len].copy_from_slice(&hash);
Ok((len, ret))
}
Expand Down
2 changes: 1 addition & 1 deletion gitoid/src/hash_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::Result;
use core::fmt;
use core::fmt::Display;
use core::fmt::Formatter;
use core::str::FromStr;
use sha1::Sha1;
use sha2::digest::DynDigest;
use sha2::Digest;
use sha2::Sha256;
use std::str::FromStr;

/// The available algorithms for computing hashes
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion gitoid/src/hash_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::fmt;
use core::fmt::Display;
use core::fmt::Formatter;
use std::ops::Deref;
use core::ops::Deref;

/// The hash produced for a `GitOid`
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion gitoid/src/object_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Error;
use core::fmt;
use core::fmt::Display;
use core::fmt::Formatter;
use std::str::FromStr;
use core::str::FromStr;

/// The types of objects for which a `GitOid` can be made.
#[repr(C)]
Expand Down

0 comments on commit 8d790c7

Please sign in to comment.