-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCastExtensions.swift
More file actions
55 lines (44 loc) · 1.11 KB
/
CastExtensions.swift
File metadata and controls
55 lines (44 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// CastExtensions.swift
// STDevRxExt
//
// Created by Tigran Hambardzumyan on 3/28/18.
//
import RxCocoa
import RxSwift
public enum RxCastError: Error {
case castFailed
}
// MARK: - Observable
public extension ObservableType {
func cast<T>(to type: T.Type) -> Observable<T?> {
return map { $0 as? T }
}
func forceCast<T>(to type: T.Type) -> Observable<T> {
return map { value in
if let casted = value as? T {
return casted
} else {
throw RxCastError.castFailed
}
}
}
}
// MARK: - SharedSequence
public extension SharedSequenceConvertibleType {
func cast<T>(to type: T.Type) -> SharedSequence<SharingStrategy, T?> {
return map { $0 as? T }
}
}
// MARK: - Single
public extension PrimitiveSequenceType where Trait == SingleTrait {
func cast<T>(to type: T.Type) -> Single<T?> {
return map { $0 as? T }
}
}
// MARK: - Maybe
public extension PrimitiveSequenceType where Trait == MaybeTrait {
func cast<T>(to type: T.Type) -> Maybe<T?> {
return map { $0 as? T }
}
}