Comments (9)
#165 added!
It's not pretty, but you can define constants right now as follows:
const UNIX_EPOCH_DELTA: Time = Time { dimension: PhantomData, units: PhantomData, value: 11644473600.0, };
from uom.
So my few days apparently turned into 20 days. I haven't forgotten about this and it's still on my to-do list. I just haven't made time to get to it yet.
from uom.
impl Zero
done in #41. Adding a zero
constant or const fn
is more complicated because trait functions cannot be const. Need some way to write const fn zero<V>() -> V
so that const fn zero() -> Quantity<..., V>
can be written.
from uom.
Since the uom
minimal Rust supported version will be bumped to 1.31 (at least it seems so based on the dev-macros
branch), it would be nice to make the {Quantity}::new
method const too, it would allow to define typed constants, ex.
const UNIX_EPOCH_DELTA: Time = Time::new::<time::second>(11644473600.0);
EDIT: I'm starting to think that this was a wrong issue to add this comment :) Do you want me to create a new issue?
from uom.
What about adding something like Quantity::new_in_base_unit()
?
impl<D, U> Quantity<D, U> {
pub const fn new_in_base_units(value: U) -> Self {
dimension: PhantomData,
units: PhantomData,
value,
}
}
That way we'd be able to write const ZERO_VELOCITY: Velocity = Velocity::new_in_base_units(0.0)
.
It's not ideal, but at least it gives people a way to create Quantity
constants and is better than people directly assigning the dimension
and units
fields. Then when const fn
in traits becomes stable, the Quantity::new()
constructor can do the generic conversion and defer to Quantity::new_in_base_units()
.
from uom.
That's an excellent idea. new_in_base_unit
or new_with_base_unit
?
If someone wants to submit a PR add the new method just below fn new
. I'll make some time in the next few days to add the method otherwise.
Lines 292 to 307 in 409252e
from uom.
I don't think it's possible with the way Quantity
is currently defined.
::: src/system.rs:243:9
|
243 | / pub struct Quantity<D, U, V>
244 | | where
245 | | D: Dimension + ?Sized,
246 | | U: Units<V> + ?Sized,
... |
256 | | pub value: V,
257 | | }
| |_________- required by `si::Quantity`
Placing $quantity::new_in_base_units()
below $quantity::new()
(in the same impl block) hits "error[E0723]: trait bounds other than Sized
on const fn parameters are unstable". That impl block requires U: Units<V>
and V: Num + Conversion<V>
.
Because the struct definition contains a where clause, any function creating a Quantity
also needs to have the same where clause. Putting $quantity::new_in_base_units()
in its own impl block gives me 312 instances of the following:
error[E0277]: the trait bound `U: si::Units<V>` is not satisfied
--> src/quantity.rs:290:56
|
290 | pub const fn new_in_base_unit(value: V) -> Self {
| ^^^^
|
::: src/si/volume_rate.rs:3:1
|
3 | / quantity! {
4 | | /// Volume rate (base unit cubic meter per second, m³ · s⁻¹).
5 | | quantity: VolumeRate; "volume rate";
6 | | /// Dimension of volume rate, L³T⁻¹ (base unit cubic meter per second, m³ · s⁻¹).
... |
149 | | }
150 | | }
| |_- in this macro invocation
|
::: src/system.rs:243:9
|
243 | / pub struct Quantity<D, U, V>
244 | | where
245 | | D: Dimension + ?Sized,
246 | | U: Units<V> + ?Sized,
... |
256 | | pub value: V,
257 | | }
| |_________- required by `si::Quantity`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `U`
|
287 | impl<U: si::Units<V>, V> $quantity<U, V> {
| ^^^^^^^^^^^^^^
I know the Rust API guidelines recommend not putting where clauses on a generic struct's declaration and only placing it on impl blocks, but to change that now would be a breaking change and a making a new major release for a single const fn
constructor is probably not worth it.
from uom.
Whats the status of this issue?
from uom.
Still starred in my inbox waiting for me to get to it unfortunately. I'm trying to get #229 merged. That in turn is blocked while I'm trying to update dependencies which uncovered some floating point precision issues.
from uom.
Related Issues (20)
- Implement operations on Dimension
- Suggestion: pass units as values HOT 2
- Suggestion: allow multiplying by a unit HOT 2
- Add Dimension type to Unit trait HOT 4
- Understand reason of 'attempt to divide by zero' HOT 2
- ElectricalPotential and Current not working with ISQ! base unit change HOT 1
- [Bug] test failure on debian HOT 5
- Change base unit of Unit not defined in the SI Quantities System but listed as SI Unit HOT 2
- Enthalpy and Entropy HOT 2
- Implement clamp HOT 2
- Unit for moving mass over distance
- Is a "Cycle" (or something similar) quantity reasonable? HOT 2
- Subtracting thermodynamic temperatures should produce a temperature interval HOT 5
- Suggestion: Using const generics to express quantities info HOT 9
- error[E0277]: the trait bound `<<D as si::Dimension>::L as std::ops::Neg>::Output: Integer` is not satisfied HOT 2
- Request: FrequencyShift HOT 2
- Support defmt framework HOT 2
- Bump MSRV HOT 3
- Support new SI prefixes
- How to use a base unit that's not predefined? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uom.