@@ -74,14 +74,16 @@ pub fn set_history_max_len(len: int) -> bool {
74
74
/// Save line history to a file
75
75
pub fn save_history ( file : & str ) -> bool {
76
76
do file. with_c_str |buf| {
77
- ( locked ! ( rustrt:: linenoiseHistorySave( buf) ) ) == 1 as c_int
77
+ // 0 on success, -1 on failure
78
+ ( locked ! ( rustrt:: linenoiseHistorySave( buf) ) ) == 0 as c_int
78
79
}
79
80
}
80
81
81
82
/// Load line history from a file
82
83
pub fn load_history ( file : & str ) -> bool {
83
84
do file. with_c_str |buf| {
84
- ( locked ! ( rustrt:: linenoiseHistoryLoad( buf) ) ) == 1 as c_int
85
+ // 0 on success, -1 on failure
86
+ ( locked ! ( rustrt:: linenoiseHistoryLoad( buf) ) ) == 0 as c_int
85
87
}
86
88
}
87
89
@@ -107,29 +109,35 @@ pub fn read(prompt: &str) -> Option<~str> {
107
109
108
110
pub type CompletionCb = @fn ( ~str , @fn ( ~str ) ) ;
109
111
110
- static complete_key: local_data:: Key < @ CompletionCb > = & local_data:: Key ;
112
+ static complete_key: local_data:: Key < CompletionCb > = & local_data:: Key ;
111
113
112
- /// Bind to the main completion callback.
114
+ /// Bind to the main completion callback in the current task .
113
115
///
114
116
/// The completion callback should not call any `extra::rl` functions
115
117
/// other than the closure that it receives as its second
116
118
/// argument. Calling such a function will deadlock on the mutex used
117
119
/// to ensure that the calls are thread-safe.
118
120
pub fn complete ( cb : CompletionCb ) {
119
- local_data:: set ( complete_key, @cb) ;
120
-
121
- extern fn callback ( line : * c_char , completions : * ( ) ) {
122
- do local_data:: get ( complete_key) |cb| {
123
- let cb = * * cb. unwrap ( ) ;
124
-
125
- unsafe {
126
- do cb ( str:: raw:: from_c_str ( line) ) |suggestion| {
127
- do suggestion. with_c_str |buf| {
128
- // This isn't locked, because `callback` gets
129
- // called inside `rustrt::linenoise`, which
130
- // *is* already inside the mutex, so
131
- // re-locking would be a deadlock.
132
- rustrt:: linenoiseAddCompletion ( completions, buf) ;
121
+ local_data:: set ( complete_key, cb) ;
122
+
123
+ extern fn callback ( c_line : * c_char , completions : * ( ) ) {
124
+ do local_data:: get ( complete_key) |opt_cb| {
125
+ // only fetch completions if a completion handler has been
126
+ // registered in the current task.
127
+ match opt_cb {
128
+ None => { } ,
129
+ Some ( cb) => {
130
+ let line = unsafe { str:: raw:: from_c_str ( c_line) } ;
131
+ do ( * cb) ( line) |suggestion| {
132
+ do suggestion. with_c_str |buf| {
133
+ // This isn't locked, because `callback` gets
134
+ // called inside `rustrt::linenoise`, which
135
+ // *is* already inside the mutex, so
136
+ // re-locking would be a deadlock.
137
+ unsafe {
138
+ rustrt:: linenoiseAddCompletion ( completions, buf) ;
139
+ }
140
+ }
133
141
}
134
142
}
135
143
}
0 commit comments