1 00:00:00,410 --> 00:00:03,590 In this video, we will use durations to 2 00:00:03,590 --> 00:00:06,590 create an ergonomic timer struct. Our 3 00:00:06,590 --> 00:00:08,720 timing code in our progress code is all 4 00:00:08,720 --> 00:00:10,940 mixed up together, we need to add some 5 00:00:10,940 --> 00:00:13,490 more timing code, so this is probably a 6 00:00:13,490 --> 00:00:15,920 good point to stop and move our timing 7 00:00:15,920 --> 00:00:19,130 code out into a nice timer struct. If 8 00:00:19,130 --> 00:00:20,269 we're going to make a timer, 9 00:00:20,269 --> 00:00:22,310 we're gonna need duration, so let's 10 00:00:22,310 --> 00:00:25,699 import that first. Next let's go down to 11 00:00:25,699 --> 00:00:27,589 the bottom of the file and create our 12 00:00:27,589 --> 00:00:30,499 timer struct there. So we'll begin with 13 00:00:30,499 --> 00:00:33,980 struct timer, and our first field will be 14 00:00:33,980 --> 00:00:36,590 last instant, we'll use it in just the 15 00:00:36,590 --> 00:00:39,500 same way we did in the loop above. Our 16 00:00:39,500 --> 00:00:42,080 second field will be our delta duration, 17 00:00:42,080 --> 00:00:45,020 we'll use this to store the value of now 18 00:00:45,020 --> 00:00:49,520 - last instant. Next, we'll store period 19 00:00:49,520 --> 00:00:52,520 which is also a duration, indicating how 20 00:00:52,520 --> 00:00:55,820 often we want the timer to go off, or be 21 00:00:55,820 --> 00:00:58,790 ready. Our next field will be countdown, 22 00:00:58,790 --> 00:01:01,430 another duration to keep track of how 23 00:01:01,430 --> 00:01:04,309 much time until the timer goes off next. 24 00:01:04,309 --> 00:01:06,920 And our final field will be ready, a 25 00:01:06,920 --> 00:01:09,439 boolean to indicate that the timer is 26 00:01:09,439 --> 00:01:12,409 ready, it's gone off. And now we're ready 27 00:01:12,409 --> 00:01:14,780 to go implement the struct. We're only 28 00:01:14,780 --> 00:01:16,759 going to need one associated function 29 00:01:16,759 --> 00:01:19,579 and one method. Let's follow convention 30 00:01:19,579 --> 00:01:21,679 and name our associated function new to 31 00:01:21,679 --> 00:01:24,469 create a new timer, we'll create now 32 00:01:24,469 --> 00:01:26,630 which will be an instant that's right 33 00:01:26,630 --> 00:01:28,729 now, and we'll use that to populate some 34 00:01:28,729 --> 00:01:31,009 of our fields. Our tail expression here 35 00:01:31,009 --> 00:01:33,530 will just be our instantiated struct. 36 00:01:33,530 --> 00:01:37,549 We'll set last instant to now, Delta to 37 00:01:37,549 --> 00:01:40,130 the durations default which is zero 38 00:01:40,130 --> 00:01:44,179 period let's set that to one second. You 39 00:01:44,179 --> 00:01:45,950 can play around with this if you want to 40 00:01:45,950 --> 00:01:49,360 see different timings, 41 00:01:49,360 --> 00:01:52,150 countdown will also set to zero via the 42 00:01:52,150 --> 00:01:55,600 default, and then ready will set to true 43 00:01:55,600 --> 00:01:58,060 because we want to get immediate 44 00:01:58,060 --> 00:02:01,180 progress output on our first read. Now we 45 00:02:01,180 --> 00:02:03,250 can move on to our one and only method, 46 00:02:03,250 --> 00:02:05,950 update. We'll need a mutable reference to 47 00:02:05,950 --> 00:02:08,020 self so that we can update the fields. 48 00:02:08,020 --> 00:02:10,060 The first thing we need to do is to get 49 00:02:10,060 --> 00:02:12,070 an instant representing right now, and 50 00:02:12,070 --> 00:02:14,350 then we can use that to calculate the 51 00:02:14,350 --> 00:02:16,209 delta and store that in the delta field. 52 00:02:16,209 --> 00:02:19,420 Then we can update the last instant 53 00:02:19,420 --> 00:02:22,209 field to be now. Now we can decrement our 54 00:02:22,209 --> 00:02:25,050 countdown and see if the timer is ready. 55 00:02:25,050 --> 00:02:28,000 Durations can't be negative so we need 56 00:02:28,000 --> 00:02:30,370 to use checked subtraction to return a 57 00:02:30,370 --> 00:02:32,260 result, depending on whether we succeed 58 00:02:32,260 --> 00:02:34,750 at doing the subtraction or overflow to 59 00:02:34,750 --> 00:02:36,400 under zero, meaning we're ready, 60 00:02:36,400 --> 00:02:39,610 so we'll map an error to a closure the 61 00:02:39,610 --> 00:02:42,370 sets ready to true, and then resets the 62 00:02:42,370 --> 00:02:45,250 countdown back to the period value, add a 63 00:02:45,250 --> 00:02:47,350 final semicolon and that's all we need 64 00:02:47,350 --> 00:02:49,780 to do, we are ready to use this timer 65 00:02:49,780 --> 00:02:52,239 struct. In the next video, 66 00:02:52,239 --> 00:02:54,730 using timer we will output progress 67 00:02:54,730 --> 00:02:59,730 statistics at a steady rate.