Skip to content

Commit 1ba5a6a

Browse files
committed
Refactor naming scheme for scm types
1 parent 141dbce commit 1ba5a6a

File tree

10 files changed

+381
-433
lines changed

10 files changed

+381
-433
lines changed

src/interp.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ffi::CString;
77
use std::mem::{transmute};
88

99
use scm::Scm;
10-
use scm::{UnspecifiedSpec, TypeSpec, SymbolSpec, ListSpec};
10+
use scm::{Untyped, TypeSpec, SymbolSpec, ListSpec};
1111

1212
#[macro_export]
1313
macro_rules! scm_eval {
@@ -85,8 +85,8 @@ impl Guile {
8585
// instead of assuming, we check before rewraping for now
8686
// NOTE: not sure if taking the key and args out of the scope of the handler is
8787
// a good idea...
88-
Err((Scm::<UnspecifiedSpec>::from_raw(err_data.1).into_symbol().unwrap(),
89-
Scm::<UnspecifiedSpec>::from_raw(err_data.2).into_list().unwrap()))
88+
Err((Scm::<Untyped>::from_raw(err_data.1).into_symbol().unwrap(),
89+
Scm::<Untyped>::from_raw(err_data.2).into_list().unwrap()))
9090
} else {
9191
Ok(Scm::<RT>::_from_raw(ret))
9292
}
@@ -101,11 +101,10 @@ impl Guile {
101101
Self::_call_with_catch(Scm::true_c(), body, body_args)
102102
}
103103

104-
pub fn eval(s: &str) -> Scm<UnspecifiedSpec> {
104+
pub fn eval(s: &str) -> Scm<Untyped> {
105105
let raw = unsafe {
106106
scm_c_eval_string(CString::new(s).unwrap().as_ptr())
107107
};
108-
Scm::<UnspecifiedSpec>::from_raw(raw)
108+
Scm::<Untyped>::from_raw(raw)
109109
}
110110
}
111-

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub mod scm;
1313
#[macro_use]
1414
pub mod interp;
1515

16-
pub use scm::{Scm, UnspecifiedSpec, NumericSpec, BoolSpec, StringSpec, IntSpec, TryAs};
16+
pub use scm::{Scm, Untyped, Numeric, Bool, Int, TryAs};
17+
pub use scm::String as ScmString;
1718
pub use interp::Guile;
1819

1920

src/scm/bool.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::ops::Not;
2+
3+
use scm::Scm;
4+
use scm::TypeSpec;
5+
6+
use guile_rs_sys::*;
7+
8+
9+
#[derive(Debug)]
10+
pub struct Bool;
11+
impl TypeSpec for Bool {}
12+
13+
impl Scm<Bool> {
14+
/// Return a true litteral Scm object
15+
#[inline]
16+
pub fn true_c() -> Scm<Bool> {
17+
Scm::_from_raw(unsafe { gu_SCM_BOOL_T() })
18+
// Scm { data: unsafe { gu_SCM_BOOL_T() } , spec: PhantomData }
19+
}
20+
21+
/// Return a false litteral Scm object
22+
#[inline]
23+
pub fn false_c() -> Scm<Bool> {
24+
Scm::_from_raw(unsafe { gu_SCM_BOOL_F() })
25+
// Scm { data: unsafe { gu_SCM_BOOL_F() }, spec: PhantomData }
26+
}
27+
28+
/// to rust boolean
29+
/// use is_true() for testing trueness
30+
pub fn to_bool(&self) -> bool {
31+
unsafe {
32+
scm_to_bool(self.data) == 1
33+
}
34+
}
35+
}
36+
37+
impl Not for Scm<Bool> {
38+
type Output = Scm<Bool>;
39+
fn not(self) -> Scm<Bool> {
40+
Scm::_from_raw(unsafe { scm_not(self.data) })
41+
}
42+
}

src/scm/mod.rs

Lines changed: 27 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! # Example
44
//! ```rust,ignore
5-
//! let s: Scm<UnspecifiedSpec> = Guile::eval("\"test string...\"");
5+
//! let s: Scm<Untyped> = Guile::eval("\"test string...\"");
66
//! let s: Scm<StringSpec> = s.into_string().unwrap();
77
//! let s: String = s.to_string();
88
//! assert_eq!(s, "test string...");
@@ -36,42 +36,25 @@ $ ($ e : tt) *
3636
$ ($ e) *
3737
}
3838
} extern crate guile_rs_sys;
39+
mod untyped;
40+
mod bool;
41+
mod string;
42+
mod numeric;
43+
pub use self :: untyped :: Untyped;
44+
pub use self :: bool :: Bool;
45+
pub use self :: string :: String;
46+
pub use self :: numeric :: *;
3947
use self :: guile_rs_sys :: *;
4048
use std :: ffi :: CString;
4149
use std :: marker :: PhantomData;
4250
use std :: ptr;
43-
use std :: ops :: Not;
4451
use std :: mem :: {
4552
transmute , forget
4653
};
4754
use std :: collections :: VecDeque;
4855
use libc;
49-
mod numeric;
5056
pub trait TypeSpec {
51-
} pub trait Numeric : TypeSpec {
52-
} # [ derive (Debug) ] pub struct UnspecifiedSpec;
53-
impl TypeSpec for UnspecifiedSpec {
54-
} # [ derive (Debug) ] pub struct BoolSpec;
55-
impl TypeSpec for BoolSpec {
56-
} /// See [spec implementation](struct.Scm.html#impl-4)
57-
# [ derive (Debug) ] pub struct NumericSpec;
58-
impl TypeSpec for NumericSpec {
59-
} impl Numeric for NumericSpec {
60-
} /// See [spec implementation](struct.Scm.html#impl-5)
61-
# [ derive (Debug) ] pub struct IntSpec;
62-
impl TypeSpec for IntSpec {
63-
} impl Numeric for IntSpec {
64-
} # [ derive (Debug) ] pub struct RationalSpec;
65-
impl TypeSpec for RationalSpec {
66-
} impl Numeric for RationalSpec {
67-
} # [ derive (Debug) ] pub struct RealSpec;
68-
impl TypeSpec for RealSpec {
69-
} impl Numeric for RealSpec {
70-
} # [ derive (Debug) ] pub struct ComplexSpec;
71-
impl TypeSpec for ComplexSpec {
72-
} impl Numeric for ComplexSpec {
73-
} # [ derive (Debug) ] pub struct StringSpec;
74-
impl TypeSpec for StringSpec {
57+
} pub trait NumericSpec : TypeSpec {
7558
} # [ derive (Debug) ] pub struct SymbolSpec;
7659
impl TypeSpec for SymbolSpec {
7760
} # [ derive (Debug) ] pub struct PairSpec;
@@ -106,13 +89,13 @@ pub (crate) data : SCM , spec : PhantomData < TS >
10689
Scm {
10790
data , spec : PhantomData
10891
}
109-
} # [ inline ] pub fn from_raw (data : SCM) -> Scm < UnspecifiedSpec > {
92+
} # [ inline ] pub fn from_raw (data : SCM) -> Scm < Untyped > {
11093
Scm :: _from_raw (data)
11194
} # [ inline ] pub unsafe fn into_raw (self) -> SCM {
11295
self . data
11396
} fn into_type < S : TypeSpec > (self) -> Scm < S > {
11497
Scm :: _from_raw (self . data)
115-
} # [ inline ] pub fn into_unspecified (self) -> Scm < UnspecifiedSpec > {
98+
} # [ inline ] pub fn into_unspecified (self) -> Scm < Untyped > {
11699
Scm :: into_type (self)
117100
} # [ inline ] pub fn as_bits (& self) -> scm_t_bits {
118101
unsafe {
@@ -150,7 +133,7 @@ is_thing_p! (list_p => scm_list_p);
150133
is_thing_p! (hash_table_p => scm_hash_table_p);
151134
/// check for identity (`scm_eq_p`)
152135
/// scheme operation: `eq?`
153-
# [ inline ] pub fn eq_p < OS : TypeSpec > (& self , other : & Scm < OS >) -> Scm < BoolSpec > {
136+
# [ inline ] pub fn eq_p < OS : TypeSpec > (& self , other : & Scm < OS >) -> Scm < Bool > {
154137
Scm :: _from_raw (unsafe {
155138
scm_eq_p (self . data , other . data)
156139
})
@@ -250,10 +233,10 @@ vals . push_back (slot_c) ;
250233
forget (slot_types);
251234
let slot_types_r : Box < Box < TypeList > > = Box :: from_raw (slot_types_r);
252235
drop (slot_types_r) ;
253-
} pub fn new_type (name : & Scm < StringSpec > , slot_names : & Scm < ListSpec > , slot_types : Box < TypeList >) -> Self {
236+
} pub fn new_type (name : & Scm < self :: String > , slot_names : & Scm < ListSpec > , slot_types : Box < TypeList >) -> Self {
254237
let slot_types : Box < Box < TypeList > > = Box :: new (slot_types);
255238
let slot_types_r : * mut Box < TypeList > = Box :: into_raw (slot_types);
256-
let slot_names : Scm < ListSpec > = Scm :: cons (& Scm :: < StringSpec > :: from ("types") , & slot_names) . into_list () . unwrap ();
239+
let slot_names : Scm < ListSpec > = Scm :: cons (& Scm :: < self :: String > :: from ("types") , & slot_names) . into_list () . unwrap ();
257240
Scm :: _from_raw (unsafe {
258241
scm_make_foreign_object_type (name . data , slot_names . data , Some (Scm :: finalizer))
259242
})
@@ -270,8 +253,8 @@ FT :: as_struct_mut ()
270253
} pub fn as_struct < 'a > () -> & 'a FT :: Struct {
271254
FT :: as_struct ()
272255
}
273-
} impl < N : Numeric > From < Scm < N > > for Scm < StringSpec > {
274-
fn from (numeric : Scm < N >) -> Scm < StringSpec > {
256+
} impl < N : NumericSpec > From < Scm < N > > for Scm < self :: String > {
257+
fn from (numeric : Scm < N >) -> Scm < self :: String > {
275258
Self {
276259
data : unsafe {
277260
scm_number_to_string (numeric . data , ptr :: null_mut ())
@@ -281,72 +264,12 @@ scm_number_to_string (numeric . data , ptr :: null_mut ())
281264
} pub trait TryAs < T , E > {
282265
/// attemp to get `&self` as type `T`
283266
fn try_as (& self) -> Result < T , E > ;
284-
} impl Scm < UnspecifiedSpec > {
285-
into_type! (into_bool , is_bool , BoolSpec);
286-
into_type! (into_string , is_string , StringSpec);
287-
into_type! (into_integer , is_integer , IntSpec);
288-
into_type! (into_symbol , is_symbol , SymbolSpec);
289-
into_type! (into_pair , is_pair , PairSpec);
290-
into_type! (into_list , is_list , ListSpec);
291-
into_type! (into_hash_table , is_hash_table , HashTableSpec);
292-
into_type! (into_hashq_table , is_hash_table , HashQTableSpec);
293-
into_type! (into_hashv_table , is_hash_table , HashVTableSpec);
294-
into_type! (into_hashx_table , is_hash_table , HashXTableSpec) ;
295-
} impl Scm < BoolSpec > {
296-
/// Return a true litteral Scm object
297-
# [ inline ] pub fn true_c () -> Scm < BoolSpec > {
298-
Scm :: _from_raw (unsafe {
299-
gu_SCM_BOOL_T ()
300-
})
301-
} /// Return a false litteral Scm object
302-
# [ inline ] pub fn false_c () -> Scm < BoolSpec > {
303-
Scm :: _from_raw (unsafe {
304-
gu_SCM_BOOL_F ()
305-
})
306-
} /// to rust boolean
307-
/// use is_true() for testing trueness
308-
pub fn to_bool (& self) -> bool {
309-
unsafe {
310-
scm_to_bool (self . data) == 1
311-
}
312-
}
313-
} impl Not for Scm < BoolSpec > {
314-
type Output = Scm < BoolSpec >;
315-
fn not (self) -> Scm < BoolSpec > {
316-
Scm :: _from_raw (unsafe {
317-
scm_not (self . data)
318-
})
319-
}
320-
} guile_impl! (impl Scm < StringSpec > {
321-
pub fn from_str < > (a0 : & str) -> Scm < StringSpec > {
322-
Scm :: _from_raw (unsafe {
323-
scm_from_utf8_string (CString :: new (a0) . unwrap () . as_ptr ())
324-
})
325-
} /// to utf8 string
326-
pub fn to_string (& self) -> String {
327-
unsafe {
328-
CString :: from_raw (scm_to_utf8_string (self . data)) . into_string () . unwrap ()
329-
}
330-
} pub fn into_symbol (self) -> Scm < SymbolSpec > {
331-
Scm :: _from_raw (unsafe {
332-
scm_string_to_symbol (self . data)
333-
})
334-
}
335-
});
336-
impl < 'a > From < & 'a str > for Scm < StringSpec > {
337-
# [ inline ] fn from (s : & 'a str) -> Scm < StringSpec > {
338-
Scm :: < StringSpec > :: from_str (s)
339-
}
340-
} impl From < String > for Scm < StringSpec > {
341-
# [ inline ] fn from (s : String) -> Scm < StringSpec > {
342-
Scm :: < StringSpec > :: from_str (& s)
343-
}
344267
} guile_impl! (impl Scm < SymbolSpec > {
345268
pub fn from_str < > (a0 : & str) -> Scm < SymbolSpec > {
346269
Scm :: _from_raw (unsafe {
347270
scm_from_utf8_symbol (CString :: new (a0) . unwrap () . as_ptr ())
348271
})
349-
} pub fn into_string < > (self) -> Scm < StringSpec > {
272+
} pub fn into_string < > (self) -> Scm < self :: String > {
350273
Scm :: _from_raw (unsafe {
351274
scm_symbol_to_string (self . data)
352275
})
@@ -357,23 +280,23 @@ impl < 'a > From < & 'a str > for Scm < SymbolSpec > {
357280
Scm :: < SymbolSpec > :: from_str (s)
358281
}
359282
} guile_impl! (impl Scm < PairSpec > {
360-
pub fn car < > (& self ,) -> Scm < UnspecifiedSpec > {
283+
pub fn car < > (& self ,) -> Scm < Untyped > {
361284
Scm :: _from_raw (unsafe {
362285
gu_scm_car (self . data)
363286
})
364-
} pub fn cdr < > (& self ,) -> Scm < UnspecifiedSpec > {
287+
} pub fn cdr < > (& self ,) -> Scm < Untyped > {
365288
Scm :: _from_raw (unsafe {
366289
gu_scm_cdr (self . data)
367290
})
368291
} pub fn cons < A : TypeSpec , B : TypeSpec > (a0 : & Scm < A > , a1 : & Scm < B >) -> Scm < PairSpec > {
369292
Scm :: _from_raw (unsafe {
370293
gu_scm_cons (a0 . data , a1 . data)
371294
})
372-
} pub fn set_car < T : TypeSpec > (& self , a0 : Scm < T >) -> Scm < UnspecifiedSpec > {
295+
} pub fn set_car < T : TypeSpec > (& self , a0 : Scm < T >) -> Scm < Untyped > {
373296
Scm :: _from_raw (unsafe {
374297
scm_set_car_x (self . data , a0 . data)
375298
})
376-
} pub fn set_cdr < T : TypeSpec > (& self , a0 : Scm < T >) -> Scm < UnspecifiedSpec > {
299+
} pub fn set_cdr < T : TypeSpec > (& self , a0 : Scm < T >) -> Scm < Untyped > {
377300
Scm :: _from_raw (unsafe {
378301
scm_set_cdr_x (self . data , a0 . data)
379302
})
@@ -390,23 +313,23 @@ gu_scm_list_n (l . as_mut_ptr ())
390313
})
391314
}
392315
} guile_impl! (impl Scm < ListSpec > {
393-
pub fn length < > (& self ,) -> Scm < IntSpec > {
316+
pub fn length < > (& self ,) -> Scm < Int > {
394317
Scm :: _from_raw (unsafe {
395318
scm_length (self . data)
396319
})
397320
} pub fn last_pair < > (& self ,) -> Scm < PairSpec > {
398321
Scm :: _from_raw (unsafe {
399322
scm_last_pair (self . data)
400323
})
401-
} pub fn m_ref < > (& self , a0 : Scm < IntSpec >) -> Scm < UnspecifiedSpec > {
324+
} pub fn m_ref < > (& self , a0 : Scm < Int >) -> Scm < Untyped > {
402325
Scm :: _from_raw (unsafe {
403326
scm_list_ref (self . data , a0 . data)
404327
})
405-
} pub fn tail < > (& self , a0 : Scm < IntSpec >) -> Scm < ListSpec > {
328+
} pub fn tail < > (& self , a0 : Scm < Int >) -> Scm < ListSpec > {
406329
Scm :: _from_raw (unsafe {
407330
scm_list_tail (self . data , a0 . data)
408331
})
409-
} pub fn head < > (& self , a0 : Scm < IntSpec >) -> Scm < ListSpec > {
332+
} pub fn head < > (& self , a0 : Scm < Int >) -> Scm < ListSpec > {
410333
Scm :: _from_raw (unsafe {
411334
scm_list_head (self . data , a0 . data)
412335
})
@@ -425,7 +348,7 @@ scm_make_hash_table (Scm :: from (a0) . data)
425348
unsafe {
426349
scm_hash_clear_x (self . data)
427350
} ;
428-
} pub fn m_ref < KS : TypeSpec , DS : TypeSpec > (& self , a0 : Scm < KS > , a1 : Option < Scm < DS > >) -> Scm < UnspecifiedSpec > {
351+
} pub fn m_ref < KS : TypeSpec , DS : TypeSpec > (& self , a0 : Scm < KS > , a1 : Option < Scm < DS > >) -> Scm < Untyped > {
429352
Scm :: _from_raw (unsafe {
430353
scm_hash_ref (self . data , a0 . data , a1 . map_or (ptr :: null_mut () , | d | d . data))
431354
})

0 commit comments

Comments
 (0)