forked from zarvd/etcd-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant.rs
57 lines (49 loc) · 1.34 KB
/
grant.rs
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
56
57
use std::time::Duration;
use crate::lease::LeaseId;
use crate::proto::etcdserverpb;
use crate::ResponseHeader;
#[derive(Debug)]
pub struct LeaseGrantRequest {
proto: crate::proto::etcdserverpb::LeaseGrantRequest,
}
impl LeaseGrantRequest {
/// Creates a new LeaseGrantRequest with the specified TTL.
pub fn new(ttl: Duration) -> Self {
Self {
proto: etcdserverpb::LeaseGrantRequest {
ttl: ttl.as_secs() as i64,
id: 0,
},
}
}
/// Set custom lease ID.
pub fn with_id(mut self, id: LeaseId) -> Self {
self.proto.id = id as LeaseId;
self
}
}
impl From<LeaseGrantRequest> for crate::proto::etcdserverpb::LeaseGrantRequest {
fn from(x: LeaseGrantRequest) -> Self {
x.proto
}
}
impl From<Duration> for LeaseGrantRequest {
fn from(ttl: Duration) -> Self {
Self::new(ttl)
}
}
#[derive(Debug, Clone)]
pub struct LeaseGrantResponse {
pub header: ResponseHeader,
pub id: LeaseId,
pub ttl: i64,
}
impl From<crate::proto::etcdserverpb::LeaseGrantResponse> for LeaseGrantResponse {
fn from(proto: crate::proto::etcdserverpb::LeaseGrantResponse) -> Self {
Self {
header: From::from(proto.header.expect("must fetch header")),
id: proto.id,
ttl: proto.ttl,
}
}
}