Skip to content

Commit

Permalink
Initial commit - early prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
electronjoe committed Apr 4, 2019
0 parents commit e1343ec
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
59 changes: 59 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
language: rust
cache:
directories:
- /home/travis/.cargo
sudo: false

rust:
- 1.31.0 # Oldest supported
- stable
- beta
- nightly
os:
- linux
- osx
- windows

# Cribbed from UOM .travis.yml
matrix:
allow_failures:
- rust: nightly
include:
- name: "Rust: 1.31.0"
rust: 1.31.0
script: |
cargo build --verbose
- name: "Rust: stable + tests"
rust: stable
script: |
cargo test --all --verbose
- name: Rustfmt
rust: 1.33.0
install:
- rustup component add rustfmt
script:
- cargo fmt -- --check
- name: Clippy
rust: 1.33.0
install:
- rustup component add clippy
script:
- RUSTFLAGS="-D warnings" cargo clippy --tests -- -D clippy::all
- name: Tarpaulin
rust: 1.33.0
sudo: required
script:
- docker run --security-opt seccomp=unconfined -v "$PWD:/volume" xd009642/tarpaulin sh -c "cargo build && cargo tarpaulin --ciserver travis-ci --coveralls $TRAVIS_JOB_ID"

install:
- rustc -Vv
- cargo -V

script: |
set -e
cargo test --verbose
before_cache:
- rm -rf /home/travis/.cargo/registry

notifications:
email:
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "piecewise"
version = "0.1.0"
authors = ["Scott Moeller <electronjoe@gmail.com>"]
edition = "2018"

[dependencies]
intervals-general = "0.1.0"

[patch.crates-io]
intervals-general = {path = "../intervals-general" }
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
format_doc_comments = true
wrap_comments = true
86 changes: 86 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use intervals_general::interval::Interval;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ValueOverInterval<T, U> {
pub(crate) interval: Interval<T>,
pub(crate) value: U,
}

impl<T, U> ValueOverInterval<T, U> {
/// Create a new ValueOverInterval
///
/// # Examples
///
/// ```
/// use intervals_general::interval::Interval;
/// use piecewise::ValueOverInterval;
///
/// let value_over_interval = ValueOverInterval::new(Interval::Unbounded::<u32> {}, 5.0);
/// ```
pub fn new(interval: Interval<T>, value: U) -> ValueOverInterval<T, U> {
ValueOverInterval { interval, value }
}

/// Fetch an immutable reference to the Interval
///
/// # Examples
///
/// ```
/// use intervals_general::bound_pair::BoundPair;
/// use intervals_general::interval::Interval;
/// use piecewise::ValueOverInterval;
/// # fn main() -> std::result::Result<(), String> {
/// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
/// let value_over_interval = ValueOverInterval::new(Interval::Closed { bound_pair: bounds }, 4);
/// assert_eq!(*value_over_interval.value(), 4);
/// # Ok(())
/// # }
/// ```
pub fn interval(&self) -> &Interval<T> {
&self.interval
}

/// Fetch an immutable reference to the value
///
/// # Examples
///
/// ```
/// use intervals_general::bound_pair::BoundPair;
/// use intervals_general::interval::Interval;
/// use piecewise::ValueOverInterval;
/// # fn main() -> std::result::Result<(), String> {
/// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
/// let value_over_interval = ValueOverInterval::new(Interval::Closed { bound_pair: bounds }, 4);
/// assert_eq!(
/// *value_over_interval.interval(),
/// Interval::Closed { bound_pair: bounds }
/// );
/// # Ok(())
/// # }
/// ```
pub fn value(&self) -> &U {
&self.value
}
}

impl<T, U, V> std::ops::Mul<V> for ValueOverInterval<T, U>
where
U: std::ops::Mul<V, Output = U>,
{
type Output = ValueOverInterval<T, U>;

fn mul(self, rhs: V) -> Self::Output {
ValueOverInterval {
interval: self.interval,
value: self.value * rhs,
}
}
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

0 comments on commit e1343ec

Please sign in to comment.