@@ -925,22 +925,41 @@ fn write_zeros_until_end_of_sector<IO: ReadWriteSeek>(disk: &mut IO, bytes_per_s
925
925
///
926
926
/// This struct implements a builder pattern.
927
927
/// Options are specified as an argument for `format_volume` function.
928
- #[ derive( Default , Debug , Clone ) ]
928
+ #[ derive( Debug , Clone ) ]
929
929
pub struct FormatVolumeOptions {
930
- pub ( crate ) bytes_per_sector : Option < u16 > ,
930
+ pub ( crate ) bytes_per_sector : u16 ,
931
931
pub ( crate ) total_sectors : Option < u32 > ,
932
932
pub ( crate ) bytes_per_cluster : Option < u32 > ,
933
933
pub ( crate ) fat_type : Option < FatType > ,
934
- pub ( crate ) max_root_dir_entries : Option < u16 > ,
935
- pub ( crate ) fats : Option < u8 > ,
936
- pub ( crate ) media : Option < u8 > ,
937
- pub ( crate ) sectors_per_track : Option < u16 > ,
938
- pub ( crate ) heads : Option < u16 > ,
934
+ pub ( crate ) max_root_dir_entries : u16 ,
935
+ pub ( crate ) fats : u8 ,
936
+ pub ( crate ) media : u8 ,
937
+ pub ( crate ) sectors_per_track : u16 ,
938
+ pub ( crate ) heads : u16 ,
939
939
pub ( crate ) drive_num : Option < u8 > ,
940
- pub ( crate ) volume_id : Option < u32 > ,
940
+ pub ( crate ) volume_id : u32 ,
941
941
pub ( crate ) volume_label : Option < [ u8 ; SFN_SIZE ] > ,
942
942
}
943
943
944
+ impl Default for FormatVolumeOptions {
945
+ fn default ( ) -> Self {
946
+ Self {
947
+ bytes_per_sector : 512 ,
948
+ total_sectors : None ,
949
+ bytes_per_cluster : None ,
950
+ fat_type : None ,
951
+ max_root_dir_entries : 512 ,
952
+ fats : 2 ,
953
+ media : 0xF8 ,
954
+ sectors_per_track : 0x20 ,
955
+ heads : 0x40 ,
956
+ drive_num : None ,
957
+ volume_id : 0x1234_5678 ,
958
+ volume_label : None ,
959
+ }
960
+ }
961
+ }
962
+
944
963
impl FormatVolumeOptions {
945
964
/// Create options struct for `format_volume` function
946
965
///
@@ -996,7 +1015,7 @@ impl FormatVolumeOptions {
996
1015
bytes_per_sector. count_ones( ) == 1 && bytes_per_sector >= 512 ,
997
1016
"Invalid bytes_per_sector"
998
1017
) ;
999
- self . bytes_per_sector = Some ( bytes_per_sector) ;
1018
+ self . bytes_per_sector = bytes_per_sector;
1000
1019
self
1001
1020
}
1002
1021
@@ -1017,7 +1036,7 @@ impl FormatVolumeOptions {
1017
1036
/// Default is `512`.
1018
1037
#[ must_use]
1019
1038
pub fn max_root_dir_entries ( mut self , max_root_dir_entries : u16 ) -> Self {
1020
- self . max_root_dir_entries = Some ( max_root_dir_entries) ;
1039
+ self . max_root_dir_entries = max_root_dir_entries;
1021
1040
self
1022
1041
}
1023
1042
@@ -1032,7 +1051,7 @@ impl FormatVolumeOptions {
1032
1051
#[ must_use]
1033
1052
pub fn fats ( mut self , fats : u8 ) -> Self {
1034
1053
assert ! ( ( 1 ..=2 ) . contains( & fats) , "Invalid number of FATs" ) ;
1035
- self . fats = Some ( fats) ;
1054
+ self . fats = fats;
1036
1055
self
1037
1056
}
1038
1057
@@ -1041,7 +1060,7 @@ impl FormatVolumeOptions {
1041
1060
/// Default is `0xF8`.
1042
1061
#[ must_use]
1043
1062
pub fn media ( mut self , media : u8 ) -> Self {
1044
- self . media = Some ( media) ;
1063
+ self . media = media;
1045
1064
self
1046
1065
}
1047
1066
@@ -1050,7 +1069,7 @@ impl FormatVolumeOptions {
1050
1069
/// Default is `0x20`.
1051
1070
#[ must_use]
1052
1071
pub fn sectors_per_track ( mut self , sectors_per_track : u16 ) -> Self {
1053
- self . sectors_per_track = Some ( sectors_per_track) ;
1072
+ self . sectors_per_track = sectors_per_track;
1054
1073
self
1055
1074
}
1056
1075
@@ -1059,7 +1078,7 @@ impl FormatVolumeOptions {
1059
1078
/// Default is `0x40`.
1060
1079
#[ must_use]
1061
1080
pub fn heads ( mut self , heads : u16 ) -> Self {
1062
- self . heads = Some ( heads) ;
1081
+ self . heads = heads;
1063
1082
self
1064
1083
}
1065
1084
@@ -1077,7 +1096,7 @@ impl FormatVolumeOptions {
1077
1096
/// Default is `0x12345678`.
1078
1097
#[ must_use]
1079
1098
pub fn volume_id ( mut self , volume_id : u32 ) -> Self {
1080
- self . volume_id = Some ( volume_id) ;
1099
+ self . volume_id = volume_id;
1081
1100
self
1082
1101
}
1083
1102
@@ -1118,12 +1137,11 @@ pub fn format_volume<S: ReadWriteSeek>(storage: &mut S, options: FormatVolumeOpt
1118
1137
trace ! ( "format_volume" ) ;
1119
1138
debug_assert ! ( storage. seek( SeekFrom :: Current ( 0 ) ) ? == 0 ) ;
1120
1139
1121
- let bytes_per_sector = options. bytes_per_sector . unwrap_or ( 512 ) ;
1122
1140
let total_sectors = if let Some ( total_sectors) = options. total_sectors {
1123
1141
total_sectors
1124
1142
} else {
1125
1143
let total_bytes: u64 = storage. seek ( SeekFrom :: End ( 0 ) ) ?;
1126
- let total_sectors_64 = total_bytes / u64:: from ( bytes_per_sector) ;
1144
+ let total_sectors_64 = total_bytes / u64:: from ( options . bytes_per_sector ) ;
1127
1145
storage. seek ( SeekFrom :: Start ( 0 ) ) ?;
1128
1146
if total_sectors_64 > u64:: from ( u32:: MAX ) {
1129
1147
error ! ( "Volume has too many sectors: {}" , total_sectors_64) ;
@@ -1133,7 +1151,7 @@ pub fn format_volume<S: ReadWriteSeek>(storage: &mut S, options: FormatVolumeOpt
1133
1151
} ;
1134
1152
1135
1153
// Create boot sector, validate and write to storage device
1136
- let ( boot, fat_type) = format_boot_sector ( & options, total_sectors, bytes_per_sector ) ?;
1154
+ let ( boot, fat_type) = format_boot_sector ( & options, total_sectors) ?;
1137
1155
if boot. validate :: < S :: Error > ( ) . is_err ( ) {
1138
1156
return Err ( Error :: InvalidInput ) ;
1139
1157
}
0 commit comments