Last active
June 14, 2022 11:40
-
-
Save ollieatkinson/2c55edc436866cabf07bb06f6616089b to your computer and use it in GitHub Desktop.
Measurement sugar for UnitInformationStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Measurement where UnitType == UnitInformationStorage { | |
@inlinable public var B: Self { converted(to: .bytes) } | |
@inlinable public var kB: Self { converted(to: .kilobytes) } | |
@inlinable public var MB: Self { converted(to: .megabytes) } | |
@inlinable public var GB: Self { converted(to: .gigabytes) } | |
@inlinable public var TB: Self { converted(to: .terabytes) } | |
@inlinable public var PB: Self { converted(to: .petabytes) } | |
@inlinable public var EB: Self { converted(to: .exabytes) } | |
@inlinable public var ZB: Self { converted(to: .zettabytes) } | |
@inlinable public var YB: Self { converted(to: .yobibytes) } | |
@inlinable public var KiB: Self { converted(to: .kibibytes) } | |
@inlinable public var MiB: Self { converted(to: .mebibytes) } | |
@inlinable public var GiB: Self { converted(to: .gibibytes) } | |
@inlinable public var TiB: Self { converted(to: .tebibytes) } | |
@inlinable public var PiB: Self { converted(to: .pebibytes) } | |
@inlinable public var EiB: Self { converted(to: .exbibytes) } | |
@inlinable public var ZiB: Self { converted(to: .zebibytes) } | |
@inlinable public var YiB: Self { converted(to: .yobibytes) } | |
public enum PivotUnitType { | |
case decimal | |
case binary | |
} | |
@inlinable public func pivot(_ by: Double, unit: PivotUnitType = .decimal) -> Self { | |
switch unit { | |
case .binary: return binary(pivot: by) | |
case .decimal: return decimal(pivot: by) | |
} | |
} | |
public func decimal(pivot: Double) -> Self { | |
if self < pivot.kB { return B } | |
if self < pivot.MB { return kB } | |
if self < pivot.GB { return MB } | |
if self < pivot.TB { return GB } | |
if self < pivot.PB { return TB } | |
if self < pivot.EB { return PB } | |
if self < pivot.ZB { return EB } | |
if self < pivot.YB { return ZB } | |
return YB | |
} | |
public func binary(pivot: Double) -> Self { | |
if self < pivot.KiB { return B } | |
if self < pivot.MiB { return KiB } | |
if self < pivot.GiB { return MiB } | |
if self < pivot.TiB { return GiB } | |
if self < pivot.PiB { return TiB } | |
if self < pivot.EiB { return PiB } | |
if self < pivot.ZiB { return EiB } | |
if self < pivot.YiB { return ZiB } | |
return YiB | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Int { | |
@inlinable public var d: Double { .init(self) } | |
@inlinable public var B: Measurement<UnitInformationStorage> { d.B } | |
@inlinable public var kB: Measurement<UnitInformationStorage> { d.kB } | |
@inlinable public var MB: Measurement<UnitInformationStorage> { d.MB } | |
@inlinable public var GB: Measurement<UnitInformationStorage> { d.GB } | |
@inlinable public var TB: Measurement<UnitInformationStorage> { d.TB } | |
@inlinable public var PB: Measurement<UnitInformationStorage> { d.PB } | |
@inlinable public var EB: Measurement<UnitInformationStorage> { d.EB } | |
@inlinable public var ZB: Measurement<UnitInformationStorage> { d.ZB } | |
@inlinable public var YB: Measurement<UnitInformationStorage> { d.YB } | |
@inlinable public var KiB: Measurement<UnitInformationStorage> { d.KiB } | |
@inlinable public var MiB: Measurement<UnitInformationStorage> { d.MiB } | |
@inlinable public var GiB: Measurement<UnitInformationStorage> { d.GiB } | |
@inlinable public var TiB: Measurement<UnitInformationStorage> { d.TiB } | |
@inlinable public var PiB: Measurement<UnitInformationStorage> { d.PiB } | |
@inlinable ublic var EiB: Measurement<UnitInformationStorage> { d.EiB } | |
@inlinable public var ZiB: Measurement<UnitInformationStorage> { d.ZiB } | |
@inlinable public var YiB: Measurement<UnitInformationStorage> { d.YiB } | |
} | |
extension Double { | |
@inlinable public var B: Measurement<UnitInformationStorage> { .init(value: self, unit: .bytes) } | |
@inlinable public var kB: Measurement<UnitInformationStorage> { .init(value: self, unit: .kilobytes) } | |
@inlinable public var MB: Measurement<UnitInformationStorage> { .init(value: self, unit: .megabytes) } | |
@inlinable public var GB: Measurement<UnitInformationStorage> { .init(value: self, unit: .gigabytes) } | |
@inlinable public var TB: Measurement<UnitInformationStorage> { .init(value: self, unit: .terabytes) } | |
@inlinable public var PB: Measurement<UnitInformationStorage> { .init(value: self, unit: .petabytes) } | |
@inlinable public var EB: Measurement<UnitInformationStorage> { .init(value: self, unit: .exabytes) } | |
@inlinable public var ZB: Measurement<UnitInformationStorage> { .init(value: self, unit: .zettabytes) } | |
@inlinable public var YB: Measurement<UnitInformationStorage> { .init(value: self, unit: .yobibytes) } | |
@inlinable public var KiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .kibibytes) } | |
@inlinable public var MiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .mebibytes) } | |
@inlinable public var GiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .gibibytes) } | |
@inlinable public var TiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .tebibytes) } | |
@inlinable public var PiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .pebibytes) } | |
@inlinable public var EiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .exbibytes) } | |
@inlinable public var ZiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .zebibytes) } | |
@inlinable public var YiB: Measurement<UnitInformationStorage> { .init(value: self, unit: .yobibytes) } | |
} |
Author
ollieatkinson
commented
Sep 10, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment