Skip to content

Commit

Permalink
refactor: replace &mut self by mut self in Integraal setters si…
Browse files Browse the repository at this point in the history
…gnatures (#22)

* replace `&mut self` by `mut self` for setters

also, replace return type to just be `Self`

* update structure usages

* add `must_use` to builder methods
  • Loading branch information
imrn99 authored Jun 2, 2024
1 parent dd304f7 commit 08086bc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
6 changes: 4 additions & 2 deletions examples/examples/montecarlo/integraal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ fn main() {
let method = ComputeMethod::MonteCarlo { n_sample: 100 };

// build the integral
let mut integral = Integraal::default();
integral.domain(domain).function(function).method(method);
let mut integral = Integraal::default()
.domain(domain)
.function(function)
.method(method);

// compute & print
let res: f64 = integral.compute().unwrap();
Expand Down
6 changes: 4 additions & 2 deletions examples/examples/rectangle/integraal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ fn main() {
let method = ComputeMethod::RectangleLeft;

// build the integral
let mut integral = Integraal::default();
integral.domain(domain).function(function).method(method);
let mut integral = Integraal::default()
.domain(domain)
.function(function)
.method(method);

// compute & print
let res: f64 = integral.compute().unwrap();
Expand Down
6 changes: 4 additions & 2 deletions examples/examples/trapezoid/integraal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ fn main() {
let method = ComputeMethod::Trapezoid;

// build the integral
let mut integral = Integraal::default();
integral.domain(domain).function(function).method(method);
let mut integral = Integraal::default()
.domain(domain)
.function(function)
.method(method);

// compute & print
let res: f64 = integral.compute().unwrap();
Expand Down
6 changes: 4 additions & 2 deletions integraal/src/structure/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ pub enum IntegraalError {
/// let method = ComputeMethod::Trapezoid;
///
/// // build the integral & compute it
/// let mut integral = Integraal::default();
/// integral.domain(domain).function(function).method(method);
/// let mut integral = Integraal::default()
/// .domain(domain)
/// .function(function)
/// .method(method);
/// assert!(integral.compute().is_ok())
/// # }
/// ```
Expand Down
9 changes: 6 additions & 3 deletions integraal/src/structure/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ use std::ops::Deref;

impl<'a, X: Scalar> Integraal<'a, X> {
/// Set the domain descriptor.
pub fn domain(&mut self, domain_descriptor: DomainDescriptor<'a, X>) -> &mut Self {
#[must_use = "unused builder struct - please remove this call"]
pub fn domain(mut self, domain_descriptor: DomainDescriptor<'a, X>) -> Self {
self.domain = Some(domain_descriptor);
self
}

/// Set the function descriptor.
pub fn function(&mut self, function_descriptor: FunctionDescriptor<X>) -> &mut Self {
#[must_use = "unused builder struct - please remove this call"]
pub fn function(mut self, function_descriptor: FunctionDescriptor<X>) -> Self {
self.function = Some(function_descriptor);
self
}

/// Set the numerical integration method.
pub fn method(&mut self, compute_method: ComputeMethod) -> &mut Self {
#[must_use = "unused builder struct - please remove this call"]
pub fn method(mut self, compute_method: ComputeMethod) -> Self {
self.method = Some(compute_method);
self
}
Expand Down
30 changes: 15 additions & 15 deletions integraal/src/structure/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ macro_rules! generate_sample_descriptors {

macro_rules! generate_missing {
($a: ident, $b: ident) => {
let mut integral: Integraal<'_, f64> = Integraal::default();
integral.$a($a).$b($b);
let mut integral: Integraal<'_, f64> = Integraal::default().$a($a).$b($b);
assert_eq!(
integral.compute(),
Err(IntegraalError::MissingParameters(
Expand Down Expand Up @@ -53,8 +52,7 @@ fn missing_parameters() {
generate_missing!(function, domain);

// missing all but one
let mut integral: Integraal<'_, f64> = Integraal::default();
integral.method(method);
let mut integral: Integraal<'_, f64> = Integraal::default().method(method);
assert_eq!(
integral.compute(),
Err(IntegraalError::MissingParameters(
Expand All @@ -70,8 +68,10 @@ fn inconsistent_parameters() {
let domain = vec![0.0, 0.1, 0.2, 0.3, 0.4]; // missing the last x value
let domain = DomainDescriptor::Explicit(&domain);

let mut integral = Integraal::default();
integral.method(method).function(function).domain(domain);
let mut integral = Integraal::default()
.method(method)
.function(function)
.domain(domain);
assert_eq!(
integral.compute(),
Err(IntegraalError::InconsistentParameters(
Expand All @@ -87,8 +87,10 @@ fn inconsistent_parameters() {
};
let function = FunctionDescriptor::Values(vec![1., 1., 1., 1., 1., 1.]);

let mut integral = Integraal::default();
integral.method(method).function(function).domain(domain);
let mut integral = Integraal::default()
.method(method)
.function(function)
.domain(domain);
assert_eq!(
integral.compute(),
Err(IntegraalError::InconsistentParameters(
Expand Down Expand Up @@ -136,12 +138,11 @@ macro_rules! generate_test {
let functiond = $fnd;
let domaind = $dmd;
let computem = $met;
let mut integraal = Integraal::default();
let res = integraal
let mut integraal = Integraal::default()
.function(functiond)
.domain(domaind)
.method(computem)
.compute();
.method(computem);
let res = integraal.compute();
assert!(res.is_ok());
assert!(
almost_equal!(res.unwrap(), 2.0, $tol),
Expand All @@ -157,8 +158,7 @@ macro_rules! generate_test {
let functiond = $fnd;
let domaind = $dmd;
let computem = $met;
let mut integraal = Integraal::default();
integraal
let integraal = Integraal::default()
.function(functiond)
.domain(domaind)
.method(computem);
Expand Down Expand Up @@ -343,7 +343,7 @@ fn B_Closure_Explicit_Rectangle() {
.collect();
let domaind = DomainDescriptor::Explicit(&domain);
let computem = ComputeMethod::RectangleLeft;
let mut integraal = Integraal::default();
let integraal = Integraal::default();
let res = integraal
.function(functiond)
.domain(domaind)
Expand Down

0 comments on commit 08086bc

Please sign in to comment.