GithubHelp home page GithubHelp logo

Comments (19)

pitdicker avatar pitdicker commented on September 26, 2024 1

I'll add a 🌞 after items when I think they are ready to convert without deep dependencies.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024 1

Thank you for everything until now 👍.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024 1

This issue is for the next major version of chrono, 0.5. All deprecated methods are removed, and the _opt and try_ methods are renamed. @ISibboI In this case you may be interested in #1515 which was just merged.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024 1

So most methods on TimeDelta are converted for that reason.
checked_add and checked_sub only fail on overflow. I have no strong opinion, but keeping a similar signature to the corresponding methods from the standard library is something to consider.

from chrono.

djc avatar djc commented on September 26, 2024

For bonus points, make this more like a dependency tree?

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

That would be cool to have, but it is more a dependency graph than a tree.

from chrono.

djc avatar djc commented on September 26, 2024

Those are kind of the same thing IMO -- unless you expect there are a bunch of cycles in there?

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

@Zomtir What would you like to work on next, if any? Then I'll stay clear.

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

Unfortunately I am occupied for the next three weeks. I will rebase the checked_(add/sub)_days once I am home and have to take a break then.

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

Well... I lied. The succ/pred caught my eye and I could not resist. Uno mas: #1513

from chrono.

ISibboI avatar ISibboI commented on September 26, 2024

Please also all the try_... APIs. Returning Option makes it impossible to use the ? operator properly, as the returned None is not connected to the chrono crate at all.

Also, please have the Err value include the erroneous input value for the try_... functions. Otherwise, functional code style becomes annoying.

Example

Currently, the try_... functions can only be used as follows:

let duration_seconds = input_function();
let duration = Duration::try_seconds(duration_seconds).ok_or(Error::TimeRangeError(duration_seconds))?;

Ideally however, it would work as follows:

impl From<ChronoTimeRangeError> for Error {
    fn from(ChronoTimeRangeError {value}: ChronoTimeRangeError) {
        Error::TimeRangeError(value)
    }
}

fn foo() -> Result<(), Error> {
    let duration = Duration::try_seconds(input_function())?;
}

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

Sorry, I was on mobile and just saw this part of your comment:

Also, please have the Err value include the erroneous input value for the try_... functions. Otherwise, functional code style becomes annoying.

Interesting example!
I'm afraid that is going to be very difficult. We are currently converting ca. 150 methods. To make them all return the erroneous input asks for multiple error types or an error type that is generic. And we can no longer simply bubble up errors within chrono but have to map them to include the input at the outermost method.
It is too big a change for me on what is already a bit complex and interwoven work to consider.

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

I will attempt TimeDelta::checked_add and TimeDelta::checked_sub now.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

Great!

Honestly we are currently out of methods that are ready to convert. Converting the parsing methods depends on #1511, and the first preparations towards converting the methods that depend on the TimeZone trait depend on #1529. As soon as those are in we can pick up work here.

I'm not sure if we want to convert TimeDelta::checked_add and TimeDelta::checked_sub. The same methods on integers return an Option. We may want to follow that for this type too. What do you think?

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

It does seem very weird to hide different errors behind None. If TimeDelta::new or to a greater extent all functions of TimeDelta returned the same error, the mapping to None would be bijective.

For integers (unit-less scalar) the overall concept might be simpler than TimeDelta, which can be converted to different units of time.

Appending .ok() also isn't too much to ask for the user in checked_add().ok().

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

Partly related, but I noticed quite a few unchecked operations in TimeDelta:

impl Mul<i32> for TimeDelta {
    type Output = TimeDelta;

    fn mul(self, rhs: i32) -> TimeDelta {
        // Multiply nanoseconds as i64, because it cannot overflow that way.
        let total_nanos = self.nanos as i64 * rhs as i64;
        let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
        let secs = self.secs * rhs as i64 + extra_secs;
        TimeDelta { secs, nanos: nanos as i32 }
    }
}

The downcasting from i64 to i32 will wrap on overflow, which should be the same result as directly multiplying two i32. Both are standard behaviour of integers, but I wonder if this is acceptable for TimeDelta.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

Do you mean nanos as i32? The result of value % NANOS_PER_SEC should always be less than NANOS_PER_SEC.

from chrono.

Zomtir avatar Zomtir commented on September 26, 2024

Do you mean nanos as i32? The result of value % NANOS_PER_SEC should always be less than NANOS_PER_SEC.

My bad, nanos are covered. let secs = self.secs * rhs could be (2^63)*(2^31) potentially? Not that it will remotely matter, just asking if this intended.

The point I'm trying to make is that integer behaviour is not always applicable, so I don't know if checked_add should be the held to that restriction. If there was a common trait that we could implement that would make more sense.

Personally I'd find internal consistency preferable, e.g having

  • add and mul return TimeDelta
  • checked_add and checked_mul return Result<TimeDelta>

But in the greater context this is splitting hairs and we should focus on the bigger targets atm. Let me know if there is something do.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

My bad, nanos are covered. let secs = self.secs * rhs could be (2^63)*(2^31) potentially? Not that it will remotely matter, just asking if this intended.

Good point! self.secs is at most something like 2^44, but that still remains an easy way to create invalid TimeDeltas and is definitely something to fix. Want to make a PR for mul? And potentially add checked_mul (against the main branch)?

from chrono.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.